简体   繁体   English

尽管在MANIFEST中没有找到类路径,但是找不到类

[英]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.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. 当类试图实例化其中一个MySQL-Library类时,我得到一个NoClassDefFoundException。

What did I do wrong? 我做错了什么?

You can't bundle jar files in other jar files. 您不能将jar文件捆绑在其他jar文件中。 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 . 清单中指定的路径相对于您正在调用的jar文件的位置,因此在您的情况mybundle.jar对于mybundle.jar的位置。

You have two options: 您有两种选择:

  1. Either put the MySQL jar in the lib directory outside of your mybundle.jar . 将MySQL jar放在mybundle.jar之外的lib目录中。
  2. Create a fat jar , which contains all classes from the required jar files in addition to your own classes. 创建一个胖jar ,除了你自己的类之外,它还包含所需jar文件中的所有类。 This is available from within Eclipse or Maven . 这可以从EclipseMaven中获得

If your mybundle.jar is in c:/foo, then your mysql.jar has be in c:/foo/lib. 如果你的mybundle.jar在c:/ foo中,那么你的mysql.jar就在c:/ foo / lib中。 The Class-Path in the manifest is relative to the executable JAR the way you've written it. 清单中的Class-Path与您编写它的方式相对于可执行JAR。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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