Create jar file from command line

I have the following directory hierarchy:

SigarTest src SigarTest .java files bin SigarTest .class files 

Here SigarTest is the name of the package. The root folder is located in the bin jdk folder. From there im ran the following command to create the jar file of my project -

 ./jar cfe temp.jar SigarTest.SigarMain SigarTest/bin/ tools.jar sigar.jar mongo-2.7.3.jar 

where tools.jar, mongo-2.7.3.jar and sigar.jar are required and are located in the same folder as the root directory (bin folder in jdk). However, at startup, I get

 ClassNotFoundException : SigarTest.SigarMain 

What am I doing wrong?

+7
source share
1 answer

Use the -C dir option, which

Temporarily changes directories ( cd dir) during jar when processing the next inputfiles argument.

If you execute the jar command in your question and list the contents of temp.jar, you will see output similar to the following:

 $ rm -rf temp.jar $ jar cfe temp.jar SigarTest.SigarMain SigarTest/bin/ tools.jar sigar.jar mongo-2.7.3.jar $ jar tf temp.jar META-INF/ META-INF/MANIFEST.MF SigarTest/bin/ SigarTest/bin/SigarTest/ SigarTest/bin/SigarTest/SigarMain.class tools.jar sigar.jar mongo-2.7.3.jar $ java -jar temp.jar Exception in thread "main" java.lang.NoClassDefFoundError: SigarTest/SigarMain Caused by: java.lang.ClassNotFoundException: SigarTest.SigarMain at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 

Note that the presence of SigarTest / bin in temp.jar is incorrect. Running temp.jar raises a ClassNotFoundException because SigarMain is in the SigarTest.bin.SigarTest package. Now consider the following jar command, which uses the -C dir option:

 $ rm -rf temp.jar $ jar cfe temp.jar SigarTest.SigarMain -C SigarTest/bin/ . tools.jar sigar.jar mongo-2.7.3.jar $ jar tf temp.jar META-INF/ META-INF/MANIFEST.MF SigarTest/ SigarTest/SigarMain.class tools.jar sigar.jar mongo-2.7.3.jar $ java -jar temp.jar 

SigarMain is in the correct package, and running temp.jar does not throw a ClassNotFoundException .

+5
source

All Articles