Why doesn't java respect the classpath when executing a jar file with the -jar switch?

It works:

$ 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>]
...

      

It does not mean:

$ 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)

      

I found the name of the class to execute (for the first example) in the META-INF / MANIFEST.MF file inside the jar:

Main-Class: com.lhf.jobexftp.StandAloneApp

      

Why does executing the jar file using the -jar switch seem to cause Java to ignore the classpath and cannot find the class gnu/io/NoSuchPortException

in RXTXcomm.jar?


On an older machine running Java 1.6, I can execute the jar file without requiring the classpath. How it works?

$ 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>]
...

      


The reason it worked on the old machine is because it had a copy of RXTXcomm.jar in / usr / lib / jvm / java -6-sun-1.6.0.21 / jre / lib / ext /.

$ 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

      

Creating a new directory ext

in / usr / lib / jvm / java -8-oracle / jre and copying RXTXcomm.jar

to it will not get rid of the error in Java 1.8.

+3


source to share


2 answers


The switch is -jar

designed to run the bank as an independent program. As stated in the man page java

:

When you use the parameter -jar

, the specified JAR file is the source of all custom classes, and other classpath parameters are ignored.



To provide a classpath, use an entry Class-Path

in your manifest file.

Further Reading: The Java ™ Tutorials: Adding Classes to the JAR Classpath

+7


source


You need to copy also .so / .dll to jni folder. Ubuntu RXTX didn't work well. Check this



-1


source







All Articles