Can't compile a module in java 9 using cmd - java

Ok, so I'm trying to learn java 9 trough a book I bought, but when I try to compile a module trough the cmd, I get this error message. image of the cmd compile error
I have been trying to understand what is happening for the last 2 days.

The problem in the screenshot you posted is that --module-source-path requires a path, identifying where the modules' source files are, so that it can compile them. Since you haven't specified where the source files are, the compiler is complaining about it.
If the project structure is
modulename/src/main/java/module-info.java
modulename/src/main/java/com/whatever/Main.java
then the command to compile it would be (run from the parent of project)
javac -d outputfolder --module-source-path "./*/src/main/java" -m modulename
The * symbolizes the module's directory.
I suggest you go through the examples in your book by following the tutorial before experimenting.

Related

J2ObjC with source jar file

I need to translate itext source jar to objective c using J2ObjC.
I use the Xcode Build rules at here .
But when I add this script
"${J2OBJC_HOME}/j2objc" --build-closure -d ${DERIVED_FILES_DIR} -sourcepath "${PROJECT_DIR}/" --no-package-directories "${PROJECT_DIR}/Classes/Othello/Engine/itext-2.1.7-sources.jar" ${INPUT_FILE_PATH};
I get build errors
This project was built and run successfully without my script.
Please help me
Thanks
Try removing the last ${INPUT_FILE_PATH}. j2objc's flags are modeled after javac's (to support build tools), so any argument after the last flag is treated as a source file. Since the sources jar holds all the source files, the ${INPUT_FILE_PATH} tells the compiler to compile a directory, which it can't do.

Javac cannot access classes within libraries

I'm currently having trouble with compiling a project I'm working on.
I use some libraries, namely lwjgl, slick and jorbis. I've added them to my classpath and I am compiling with the following command
javac -g -classpath ".:./lib/lwjgl.jar:./lib/slick.jar:./lib/jorbis-0.0.17-2.jar" -d ./bin #files
The files file is simply a list of every class I'd like to compile.
The compiler then throws a lot of errors like:
foo\bar.java:25 cannot find symbol
bar\foo.java:3 package does not exist
What's especially strange is that compilation worked on a previous project where I only used one library instead of multiple.
I use Sublime Text 3 and bash on Linux Mint to code / compile my work (if that helps.)
All help is very welcome =]
[EDIT]:
I got the project to compile now.
I also tried to compile this on Windows (using git bash) and I get the same errors, but not on Linux anymore. I'll try to compile using a batch script on Windows, but at least I can compile it =]
Also works when I pack the finished project into a .jar.
Thanks y'all.

Running a java project in terminal with multiple .java files (multiple attempts described inside)

I have been doing a coding exercise inside the IntelliJ IDEA Community Edition 14 IDE, using OpenJDK.
The project is split over 4 .java files all in the same package.
My end goal is to run this in the terminal/bash (I use System.console().readLine() which doesnt play nicely in the IDE's console).
I've tried navigating to the directory where these 4 files reside (they all reside in the same dir) and tried:
javac -classpath . BibliotecaApp.java Book.java BookManager.java LibraryDB.java
This creates 4 corresponding .class files fine.
The Main is in bibliotecaApp.java/class, so I try run it by:
java BibliotecaApp
But I get the error
Exception in thread "main" java.lang.NoClassDefFoundError: BibliotecaApp (wrong name: com/twu/biblioteca/BibliotecaApp)
Plus about 13 lines of specifics.
Now googling this error seems to be a class path problem, and this is where I get stuck.
From places I've read, usingecho $PATH gives me:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
This is still from the directory of the .java files above.
That path means nothing to me. I have no idea what it is or what it does, or what even a classpath is! Theres alot of resources out there on setting a classpath, but they aren’t helping me because I don't know what it's meant for.
That was a dead end for me. I tried to create a .jar instead using IDEA's Build Artifacts as seen HERE. It created a .jar file but when I navigate to that directory and try run it via:
java -jar biblioteca_jar
I get
Error: Invalid or corrupt jarfile biblioteca_jar
Another issue is that in the file explorer, the file actually comes out as biblioteca.jar, but ls on that dir shows biblioteca_jar. Is that normal?
The code is on my GitHub if that helps anything
https://github.com/botagar/twu-biblioteca-JohnGeddes
Based on your compiler step, change this
java BibliotecaApp
to
java -cp . BibliotecaApp
Which will add the current directory to the classpath for the java runtime environment. See also the Oracle Technote Setting the Class Path.
A jar file is a kind of zip, and should have a .jar extension. So this
java -jar biblioteca_jar
should probably be
java -jar biblioteca.jar
And you can check if the file is valid with any zip archive reader. Including jar itself,
jar tvvf biblioteca.jar
Edit
Based on your comments below,
cd ~/Documents/ThoughtWorks Uni/TWU_Biblioteca-master/src/
and then
java -cp . com.twu.biblioteca.BibliotecaApp

how to run java program from flash drive that has classes in package, from command line?

I have an assignment where I have to submit my code in flash drive.
I did all coding in eclipse and getting all output. I tried running it through command line and it works. I copied all java files and class files to flash drive and tried again through command line it works in my laptop. but when I take that dive and put it my desktop computer and run through command line, it can not recognise the classes that are in the package.
I have a package a1.cis568 under this package I have several classes. main class is A1 and other classes are Circle, Point, PlaneCircle, EHashtable, CHashtable in the same package.
I have to use following line on command line to compile and run my program through flash drive, (don't have to change any classpath while doing so)
E:>
E:>javac -d . A1.java
E:>java a1.cis568.A1
When I was searching for solution I found that I can install JDK on flash drive and run the code. I tried installing JDK but it doesn't solve my problem.
If you are using eclipse you can easily export the project as a runnable jar. Copy the jar file and source to your flash drive. Execute from command line via the following
java -jar [runnable jar filename]
You seem to have troubles compiling your source code!
Since you navigate trought the package hierarchy to where A1.java is located and then try to compile, the compiler cannot find other classes which A1 depends on and which are not in the same directory. You have to tell it where to find them, this done using javac's option -sourcepath.
So from the same location of the class A1.java you could compile your code using something like this:
E:> javac -d . A1.java -sourcepath ..\..\
Here ..\..\ is used to tell that the sources can be found two directories above the actual directroy (based on your package tree a1\cis568\).
But to make things easy I would suggest you compile from the root of you source code tree. In this case the sourcepath and classpath are the actual working directory per default.
So navigate in the commad prompt to the location where the package/directory a1 is, then compile like this:
E:> javac -d . a1\cis568\A1.java
The comipiler should be able to find all classes on which A1 depends on (assuming they all exist with the correct package inside the root of your source code).
Run with
E:> java a1.cis568.A1

Where do java packages live on a linux system? Package org.json does not exist Error using javac [duplicate]

This question already has answers here:
What is a classpath and how do I set it?
(10 answers)
Closed last year.
I am trying to compile a library I wrote, using javac and I am getting the error: package org.json does not exist. My program includes org.json.JSONArray and org.json.JSONException.
I have this package installed on my computer because I have successfully compiled android apps that import org.json libraries. I'm pretty sure all I have to do is specify a -classpath but I have been unable to find where these files live on my system (ubuntu 10.10 64-bit sun-java6).
Having been unable to find these on my own system I downloaded the org.json files from here, but I was unable to compile them individually because they were co-dependent on each other.
So I have a couple questions:
Does anyone know where the org.json
package lives from android sdk
install?
Where might I find a tutorial
explaining these basic concepts
regarding compiling, and javac.
Whatever external jars you need to compile with should be on the classpath when you compile. The most non-invasive way to do this is do add these items to the javac command line such as
javac -classpath /path/to/json.jar;. -g YourClass.java
or more likely if you use an IDE, add these jars to your referenced jars of the project in your IDE.
It usually isn't a good idea to pollute the global $CLASSPATH variable, as this then gets pulled in for everything you do with java, which may cause unintended conflicts.
Wherever you like. What you need to do is examine your CLASSPATH variable, and make sure it includes the directory with your library.
Here's the first thing:
$ echo $CLASSPATH
and you'll see your classpath as it is.
Now you need to find the jar file containing the org.json; consult the documentation, but it may be something as simple as json.jar. On most LINUX systems you can then just run
$ locate json.jar
And you'll get a path name for the jarfile. Make sure that path is part of your CLASSPATH and you'll be in fat city.
Oh, and the "Getting started" tutorials at Sun Oracle are the easiest place to start.
Actually, having looked at the files, they may not be packaged as a jar file. In that case, you want to put them into your sources starting at some top directory (src in this example.)
/src
/org/json/ ... put the json files here
... put your files here
and when you compile, they'll all be included, which will resolve all the dependencies.
Again, the place to look for first steps is that tutorial.
use "java" command instead of "javac"

Categories

Resources