为什么在使用 -jar 开关执行 jar 文件时 java 不遵循 class 路径?
Why doesn't java honour the class path when executing a jar file with the -jar switch?
这个有效:
$ java -cp ".:/PATH/TO/RXTXcomm.jar:./jobexftp.jar" -Djava.library.path=/usr/lib/jni com.lhf.jobexftp.StandAloneApp
JObexFTP 2.0 beta (15/10/2010)
Java Obex File Transfer Protocol application and library
Developed under/using 100% free software.
For more information access: http://www.lhf.ind.br/jobexftp/
Usage: jobexftp <serialPort> [<commands>] [<options>]
...
这不是:
$ java -cp ".:/PATH/TO/RXTXcomm.jar" -Djava.library.path=/usr/lib/jni -jar jobexftp.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: gnu/io/NoSuchPortException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
我在 jar 中的 META-INF/MANIFEST.MF 文件中找到要执行的 class 名称(对于第一个示例):
Main-Class: com.lhf.jobexftp.StandAloneApp
为什么使用 -jar 开关执行 jar 文件似乎导致 Java 忽略 class 路径并且无法找到 gnu/io/NoSuchPortException
class RXTXcomm.jar?
在较旧的机器上,运行 Java 1.6,我可以在不需要 class 路径的情况下执行 jar 文件。这是如何工作的?
$ env | grep CLASS
$ env | grep JAVA
$ java -jar jobexftp.jar
JObexFTP 2.0 beta (15/10/2010)
Java Obex File Transfer Protocol application and library
Developed under/using 100% free software.
For more information access: http://www.lhf.ind.br/jobexftp/
Usage: jobexftp <serialPort> [<commands>] [<options>]
...
它在旧机器上工作的原因是它在 /usr/lib/jvm/java-6-sun-1.6.0.21/jre/lib/ext/.
中有一个 RXTXcomm.jar 的副本
$ ls -lA /usr/lib/jvm/java-6-sun-1.6.0.21/jre/lib/ext/RXTXcomm.jar
-rwxr-xr-x 1 root root 137764 2011-02-17 16:58 /usr/lib/jvm/java-6-sun-1.6.0.21/jre/lib/ext/RXTXcomm.jar
在 /usr/lib/jvm/java-8-oracle/jre 中创建一个新的 ext
目录并将 RXTXcomm.jar
复制到其中 不会消除 [=] 上的错误44=] 1.8。
-jar
开关旨在将 jar 作为自包含程序启动。如 java
手册页所述:
When you use the -jar
option, the specified JAR file is the source of all user classes, and other class path settings are ignored.
要提供类路径,请使用清单文件中的 Class-Path
条目。
进一步阅读:The Java™ Tutorials: Adding Classes to the JAR File's Classpath
您还需要将 .so/.dll 复制到 jni 文件夹中。
Ubuntu RXTX 过去工作不顺畅。
查看 this
您可以修改 META-INF/MANIFEST.MF
文件并添加 Class-Path
条目。
在 maven 构建系统中,您可以像下面这样配置它
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addClasspath>true</addClasspath>
<mainClass>your.main.Class</mainClass>
</manifest>
<manifestEntries>
<Class-Path>./config/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
从man java
我们可以确认这个问题When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
这个有效:
$ java -cp ".:/PATH/TO/RXTXcomm.jar:./jobexftp.jar" -Djava.library.path=/usr/lib/jni com.lhf.jobexftp.StandAloneApp
JObexFTP 2.0 beta (15/10/2010)
Java Obex File Transfer Protocol application and library
Developed under/using 100% free software.
For more information access: http://www.lhf.ind.br/jobexftp/
Usage: jobexftp <serialPort> [<commands>] [<options>]
...
这不是:
$ java -cp ".:/PATH/TO/RXTXcomm.jar" -Djava.library.path=/usr/lib/jni -jar jobexftp.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: gnu/io/NoSuchPortException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
我在 jar 中的 META-INF/MANIFEST.MF 文件中找到要执行的 class 名称(对于第一个示例):
Main-Class: com.lhf.jobexftp.StandAloneApp
为什么使用 -jar 开关执行 jar 文件似乎导致 Java 忽略 class 路径并且无法找到 gnu/io/NoSuchPortException
class RXTXcomm.jar?
在较旧的机器上,运行 Java 1.6,我可以在不需要 class 路径的情况下执行 jar 文件。这是如何工作的?
$ env | grep CLASS
$ env | grep JAVA
$ java -jar jobexftp.jar
JObexFTP 2.0 beta (15/10/2010)
Java Obex File Transfer Protocol application and library
Developed under/using 100% free software.
For more information access: http://www.lhf.ind.br/jobexftp/
Usage: jobexftp <serialPort> [<commands>] [<options>]
...
它在旧机器上工作的原因是它在 /usr/lib/jvm/java-6-sun-1.6.0.21/jre/lib/ext/.
中有一个 RXTXcomm.jar 的副本$ ls -lA /usr/lib/jvm/java-6-sun-1.6.0.21/jre/lib/ext/RXTXcomm.jar
-rwxr-xr-x 1 root root 137764 2011-02-17 16:58 /usr/lib/jvm/java-6-sun-1.6.0.21/jre/lib/ext/RXTXcomm.jar
在 /usr/lib/jvm/java-8-oracle/jre 中创建一个新的 ext
目录并将 RXTXcomm.jar
复制到其中 不会消除 [=] 上的错误44=] 1.8。
-jar
开关旨在将 jar 作为自包含程序启动。如 java
手册页所述:
When you use the
-jar
option, the specified JAR file is the source of all user classes, and other class path settings are ignored.
要提供类路径,请使用清单文件中的 Class-Path
条目。
进一步阅读:The Java™ Tutorials: Adding Classes to the JAR File's Classpath
您还需要将 .so/.dll 复制到 jni 文件夹中。 Ubuntu RXTX 过去工作不顺畅。 查看 this
您可以修改 META-INF/MANIFEST.MF
文件并添加 Class-Path
条目。
在 maven 构建系统中,您可以像下面这样配置它
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addClasspath>true</addClasspath>
<mainClass>your.main.Class</mainClass>
</manifest>
<manifestEntries>
<Class-Path>./config/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
从man java
我们可以确认这个问题When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.