Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you compile against a jar file in Java 9?

Java 9 introduced the concept of modules, which do not explicitly replace jar files, but the old --classpath option seems to be gone.

The command javac --help no longer mentions the classpath.

I am trying to compile some student work against JUnit:

> javac *.java --classpath junit.jar

But I get a variety of errors, depending on what I try. For the example above:

javac: invalid flag: --classpath

My Java version is:

> java --version
java 9
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)

I'm not trying to use a module, just a .jar file that I could include in IntelliJ by specifying a libs folder.

Edit: My real problem was that I was not including the correct jars. Compiling against IntelliJ's default JUnit5 configuration worked:

 javac -classpath "C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.5\plugins\junit\lib\junit-jupiter-api-5.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.5\plugins\junit\lib\opentest4j-1.0.0.jar" *.java
like image 623
Josiah Yoder Avatar asked May 15 '18 16:05

Josiah Yoder


People also ask

How can I tell which JAR file is compiled?

Download a hex editor and open one of the class files inside the JAR and look at byte offsets 4 through 7. The version information is built in. Note: As mentioned in the comment below, those bytes tell you what version the class has been compiled FOR, not what version compiled it.

How do I compile a Java classpath file?

Compile Java program using -classpath option: javac -classpath ~/jjava/class -d ~/jjava/class TimeTest. java<cr>--This command tells the javac to compile TimeTest. java and put the resulted bytecodes in ~/jjava/class/.


1 Answers

You misspelled the option. According to the javac options page for Java 9:

--class-path path , -classpath path, or -cp path

Specifies where to find user class files and annotation processors. This class path overrides the user class path in the CLASSPATH environment variable.

For some reason, if you use the two hyphens option --, there is a hyphen in "classpath": --class-path. The one hyphen option - has no hyphen in "classpath": -classpath.

Any of these are valid:

javac *.java --class-path junit.jar

javac *.java -classpath junit.jar

javac *.java -cp junit.jar
like image 128
rgettman Avatar answered Sep 30 '22 01:09

rgettman