I'm trying to use the public methods/classed from a project provided as a jar file (called Hello.jar for instance) wrapped in a package called hello.
package hello; public class Hello { public static void main(String[] args) { coucou(); } public static void coucou() { System.out.println("Hello there"); } }
In a separate project called Tool, I want to be able to call the method Hello.coucou()
so I wrote something like this:
import hello.*; public class Tool { public static void main(String[] args) { System.out.println("main program running"); Hello.coucou(); } }
and I compiled Tool.java with the following command (under linux):
$ javac Tool.java -classpath .:./extern/:
where Hello.jar is located in the folder ./extern
This seems to compile fine but when I launch it (i.e. java Tool), I get this:
main program running Exception in thread "main" java.lang.NoClassDefFoundError: hello/Hello at Tool.main(Tool.java:9) Caused by: java.lang.ClassNotFoundException: hello.Hello at java.net.URLClassLoader$1.run(URLClassLoader.java:217) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:323) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) at java.lang.ClassLoader.loadClass(ClassLoader.java:268) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336) ... 1 more
I am new to Java (C/C++ background) and I don't understand what I'm doing wrong. Any ideas?
Cheers David
Edit: I tried adding Hello.jar to the classpath on the command line, but I still get the same error:
$ javac Tool.java -classpath .:./extern/Hello.jar: $ java Tool -classpath .:./extern/Hello.jar: main program running Exception in thread "main" java.lang.NoClassDefFoundError: hello/Hello at Tool.main(Tool.java:9) Caused by: java.lang.ClassNotFoundException: hello.Hello at java.net.URLClassLoader$1.run(URLClassLoader.java:217) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:323) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) at java.lang.ClassLoader.loadClass(ClassLoader.java:268) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336) ... 1 more