Java – create jar files from the command line

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 located in the bin folder of the JDK From there, I run the following command to create the jar file for 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 is required and is located in the same folder as the root directory (bin folder of JDK) However, after running it, I got

ClassNotFoundException : SigarTest.SigarMain

What on earth did I do wrong?

Solution

Use - C dir option which @ h_ 301_ 19@

Temporarily changes directories (cd dir) during execution of the jar command while processing the following inputfiles argument.

If you execute the jar command in the problem and list 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 in temp It is incorrect to use sigartest / bin in jar Execute temp Jar will throw classnotfoundexception because sigarmain is located in package sigartest bin. Sigartest Now consider the following jar command using the - CIR 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, execute temp Jar will not throw classnotfoundexception

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>