使用-jar开关执行jar文件时,为什么java不尊重类路径?
作者:互联网
这有效:
$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文件中执行的类名(对于第一个示例):
Main-Class: com.lhf.jobexftp.StandAloneApp
为什么使用-jar开关执行jar文件似乎会导致Java忽略类路径并且无法在RXTXcomm.jar中找到gnu / io / NoSuchPortException类?
在运行Java 1.6的旧机器上,我可以执行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复制到它中并没有消除Java 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
标签:java,jar,classpath,rxtx 来源: https://codeday.me/bug/20190727/1558393.html