Creating jar from package structure - noPEx/Packaging GitHub Wiki
$pwd /home/user/tut
$ ls -R .: com
./com: localhost
./com/localhost: Box.java
We want to make it reusable, keep it in a jar file $ javac com/localhost/Box.java
Note : com/local/Box.java should have the line package com.localhost; To make a jar out of it. do not change the directory , i.e $pwd /home/user/tut
$jar cvf box.jar com/localhost/Box.class added manifest adding: com/localhost/Box.class(in = 346) (out= 255)(deflated 26%) Now to use it, move the jar file to a folder say /home/user/jartest
$pwd /home/user/jartest
$ls
box.jar User.java #the same jar that we created above
$cat User.java
import com.localhost.Box;
public class User {
public static void main(String args[]) {
Box b = new Box();
System.out.println("box is successful here");
}
}
$ javac -cp .:box.jar User.java
To Run:
$ java User
Exception in thread "main" java.lang.NoClassDefFoundError: com/localhost/Box at User.main(User.java:5)
Caused by: java.lang.ClassNotFoundException: com.localhost.Box
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
This is wrong because you have to mention the path of the box.jar. Let us do it again.
$java -cp .:box.jar User box is successful here