DesirePRG
DesirePRG

Reputation: 6378

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

Following is the manifest file of a jar I created.

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.

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

Upvotes: 0

Views: 4327

Answers (2)

Kedar Mhaswade
Kedar Mhaswade

Reputation: 4695

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,

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.

If you see the JAR file tutorial, it clearly mentions the following:

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. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. 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.

You should make those other JAR files available on a local folder. If you like Maven, perhaps consider using the maven shade plugin.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691865

Although the Class-Path entry shows that it has added necessary jars in the lib folder of the jar

Java doesn't look for jar files inside your jar file. 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. Those should be used by test code, not production code.

Upvotes: 2

Related Questions