Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following directory hierarchy :

SigarTest
 src
    SigarTest
     .java files
 bin
    SigarTest
     .class files

Here,SigarTest is a package name. The root folder is in the bin folder of the jdk. From there, im running the following command to create a 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 in the same folder as root directory (bin folder of jdk). However, on running it, i get

ClassNotFoundException : SigarTest.SigarMain

What am i doing wrong ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
233 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use the -C dir option which

Temporarily changes directories (cd dir) during execution of the jar command while processing the following 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 having SigarTest/bin in temp.jar is incorrect. Executing temp.jar throws the ClassNotFoundException since SigarMain is in package SigarTest.bin.SigarTest. Now consider the following jar command that 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 executing temp.jar does not throw a ClassNotFoundException.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...