Reputation: 4522
After reading this question I have managed to run a .jar file that had external dependencies locate in jar files:
/usr/lib/jvm/java-8-jdk/bin/java -classpath /usr/local/bin/kiaragen.jar:/home/kiara/AppLab/KIARA/kiaragen/src/main/resources/org/fiware/kiara/generator/idl/templates/*:/usr/lib/jvm/java-8-jdk/jre/lib/*:/home/kiara/AppLab/KIARA/kiaragen/lib/* org.fiware.kiara.generator.kiaragen
where /usr/local/bin/kiaragen.jar is the file to execute. Now, I'm trying to run a different version that depends on an a .class file:
/home/kiara/AppLab/KIARA/IDL-Parser/target/classes/com/eprosima/idl/parser/exception/ParseException.class
Adding the file to the classpath:
/usr/lib/jvm/java-8-jdk/bin/java -classpath /usr/local/bin/kiaragen-0.2.0.jar:/home/kiara/AppLab/KIARA/IDL-Parser/target/classes/com/eprosima/idl/parser/exception/ParseException.class:/home/kiara/AppLab/KIARA/kiaragen/src/main/resources/org/fiware/kiara/generator/idl/templates/*:/usr/lib/jvm/java-8-jdk/jre/lib/*:/home/kiara/AppLab/KIARA/kiaragen/lib/* org.fiware.kiara.generator.kiaragen
gives the following exception:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/eprosima/idl/parser/exception/ParseException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: com.eprosima.idl.parser.exception.ParseException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Why?
The manifest file of the .jar is the same as before:
$ cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
Main-Class: org.fiware.kiara.generator.kiaragen
How can I add the .class file to the class path?
Upvotes: 0
Views: 1212
Reputation: 691715
You're not understanding what the classpath is. The classpath is a collection of jar files and directories where Java looks for classes (and other resources loaded by the class loader).
If a program uses the class com.foo.Bar
, what must be in the classpath is not the file /somedirectory/com/foo/Bar.class
. What must be in the classpath is the directory /somedirectory
. Or the jar file containing that class.
From that base directory or jar file, the class loader will then look for a file corresponding to the class name:
com.foo.Bar --> com/foo/Bar.class
This is essential, because it allows Java to have access to hundreds of classes at once, without having to list those hundreds of class files in the classpath. All you need in the classpath is the directory or jar containing those hundreds of classes.
Also note that /usr/lib/jvm/java-8-jdk/jre/lib/*
must not be inthe classpath either. Java knows where to find the libraries of the JRE itself.
Upvotes: 5