简体   繁体   中英

Class not found despite of classpath in MANIFEST

I have this jar:

/mybundle.jar/
    de/mybundle/myclass.class
    lib/mysql.jar
    META-INF/MANIFEST.MF

With the following MANIFEST.MF

Manifest-Version: 1.0
Class-Path: lib/mysql.jar
Main-Class: de.mybundle.myclass

It all seems perfectly correct for me, but when I run

java -jar mybundle.jar

I get a NoClassDefFoundException when the class tries to instantiate one of the MySQL-Library classes.

What did I do wrong?

You can't bundle jar files in other jar files. The paths specified in the Manifest are relative to the location of the jar file you're calling, so in your case relative to the location of mybundle.jar .

You have two options:

  1. Either put the MySQL jar in the lib directory outside of your mybundle.jar .
  2. Create a fat jar , which contains all classes from the required jar files in addition to your own classes. This is available from within Eclipse or Maven .

If your mybundle.jar is in c:/foo, then your mysql.jar has be in c:/foo/lib. The Class-Path in the manifest is relative to the executable JAR the way you've written it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM