Jar in classpath but still class not found
Sorry, this seems so obvious, but I can't find why it doesn't work.
1. Class MyPrinter in package mytest
package mytest;
public class MyPrinter {
public void print(){
System.out.println("Hello!");
}
}
2. Class PrintTest imports package mytest.MyPrinter and uses it. Note
PrintTest is in default package.
import mytest.MyPrinter;
public class PrintTest {
public static void main(String[] args) {
MyPrinter ps = new MyPrinter();
ps.print();
}
}
I export the package mytest as a Printer.jar file:
META-INF/MANIFEST.MF contains just:
Manifest-Version: 1.0
Printer.jar contains a folder mytest with the compiled class
MyPrinter.class in it.
Now I want to run PrintTest using that compiled file in the jar, so I
compile it:
$ javac -cp Printer.jar PrintTest.java
Compilation succeeds and PrintTest.class is created.
Now executing:
$ java -cp Printer.jar PrintTest
Exception in thread "main" java.lang.NoClassDefFoundError: PrintTest
Caused by: java.lang.ClassNotFoundException: PrintTest
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: PrintTest. Program will exit.
But this works!
$ java -Xbootclasspath/a:Printer.jar PrintTest
Hello!
What am I doing wrong?
Thanks Phil