简体   繁体   English

如何在Java 9中针对jar文件进行编译?

[英]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. Java 9引入了模块的概念,这些模块不会显式替换jar文件,但是旧的--classpath选项似乎已消失。

The command javac --help no longer mentions the classpath. 命令 javac --help不再提及类路径。

I am trying to compile some student work against JUnit: 我正在尝试针对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版本是:

> 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. 我不是在尝试使用模块,而只是通过指定libs文件夹将其包含在IntelliJ中的.jar文件。

Edit : My real problem was that I was not including the correct jars. 编辑 :我真正的问题是我没有包括正确的罐子。 Compiling against IntelliJ's default JUnit5 configuration worked: 使用IntelliJ的默认JUnit5配置进行编译可以:

 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

You misspelled the option. 您拼错了该选项。 According to the javac options page for Java 9 : 根据Java 9javac选项页面

--class-path path , -classpath path , or -cp path --class-path 路径-classpath 路径-cp 路径

Specifies where to find user class files and annotation processors. 指定在何处查找用户类文件和注释处理器。 This class path overrides the user class path in the CLASSPATH environment variable. 该类路径将覆盖CLASSPATH环境变量中的用户类路径。

For some reason, if you use the two hyphens option -- , there is a hyphen in "classpath": --class-path . 由于某些原因,如果您使用两个连字符选项-- ,则“类路径”中会有一个连字符:-- --class-path The one hyphen option - has no hyphen in "classpath": -classpath . 一个连字符选项- “ classpath”中没有连字符: -classpath

Any of these are valid: 以下任何一项均有效:

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

javac *.java -classpath junit.jar

javac *.java -cp junit.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM