简体   繁体   English

即使在清单类路径中包含了JAR,也不会出现NoClassDefFoundError

[英]NoClassDefFoundError even though the JAR's are included in Manifest Class-Path

Following is the manifest file of a jar I created. 以下是我创建的jar的清单文件。

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: Arun
Class-Path: lib/httpclient-4.5.1.jar lib/httpcore-4.4.3.jar lib/c
 ommons-logging-1.2.jar lib/commons-codec-1.9.jar lib/httpmime-4.5.1.j
 ar lib/json-simple-1.1.1.jar lib/junit-4.10.jar lib/hamcrest-core-1.1
 .jar
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_72
Main-Class: org.wso2.ml.driver.Shell

Although the Class-Path entry shows that it has added necessary jars in the lib folder of the jar, when I run this executable jar I get a Noclassdeffound error. 尽管Class-Path条目显示它已在jar的lib文件夹中添加了必需的jar,但是当我运行此可执行jar时,出现了Noclassdeffound错误。

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/entity/mime/content/ContentBody

This is how I execute my jar 这就是我执行我的罐子的方式

java -jar restclient-1.0-SNAPSHOT.jar

Although the Class-Path entry shows that it has added necessary jars in the lib folder of the jar 尽管Class-Path条目显示它已在jar的lib文件夹中添加了必要的jar

Java doesn't look for jar files inside your jar file. Java不找你的jar文件的jar文件。 It looks for them next to the jar. 它在罐子旁边寻找它们。 So you need this file/directory structure: 因此,您需要以下文件/目录结构:

restclient-1.0-SNAPSHOT.jar
lib/
  httpclient-4.5.1.jar 
  httpcore-4.4.3.jar
  ...

Also, having junit and hamcrest in your runtime dependencies doesn't seem right. 另外,在运行时依赖项中包含junit和hamcrest似乎也不对。 Those should be used by test code, not production code. 这些应该由测试代码而不是生产代码使用。

I believe that the mistake is the assumption you seem to be making: 我认为错误是您似乎正在做出的假设:

Although the Class-Path entry shows that it has added necessary jars in the lib folder of the jar , 尽管Class-Path条目显示它已在jar的lib文件夹添加了必需的jar

I am assuming that you have put these Class-Path JAR's in the restclient-1.0-SNAPSHOT.jar itself for otherwise you'd not be having this problem. 我假设您已将这些Class-Path JAR放在restclient-1.0-SNAPSHOT.jar本身中,否则您将不会遇到此问题。

If you see the JAR file tutorial , it clearly mentions the following: 如果您看到JAR文件教程 ,它将明确提到以下内容:

Note: The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over Internet protocols. 注意:Class-Path标头指向本地网络上的类或JAR文件,而不是JAR文件中的JAR文件或可通过Internet协议访问的类。 To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. 要将JAR文件中的JAR文件中的类加载到类路径中,必须编写自定义代码以加载这些类。 For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path. 例如,如果MyJar.jar包含另一个名为MyUtils.jar的JAR文件,则不能使用MyJar.jar清单中的Class-Path标头将MyUtils.jar中的类加载到类路径中。

You should make those other JAR files available on a local folder. 您应该使那些其他JAR文件在本地文件夹中可用。 If you like Maven, perhaps consider using the maven shade plugin . 如果您喜欢Maven,也许考虑使用maven shade plugin

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

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