pstatix
pstatix

Reputation: 3858

jar throwing NoClassDefFoundError

I've got a project that is structured liked so:

root/
-- lib/
---- commons-cli-1.2.jar
-- src/
---- my/package/name/*.java
-- bin/
---- my/package/name/*.class
-- .classpath
-- .project
-- manifest.mf

The *.class files in bin/ are made by Eclipse each build. My .classpath has the lib/ included and compiles just fine; it also runs as a "Java Application" just fine with my current stubs.

The issue comes about when I try to create a JAR and include the lib/ dependencies. From the command line I've been issuing:

jar cvfm prog.jar manifest.mf -C bin/ .

The program builds, and then when I try to run java -jar prog.jar, I get:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/Option... (there is more, but I cant copy from the other machine)

My manifest.mf looks like:

Manifest-Version: 1.0
Main-Class: my.package.Main
Class-Path: ./lib/commons-cli-1.2.jar

Seems to me that the Class-Path isn't being included and the JAR cannot find the classes contained in the commons-cli-1.2.jar. I've isolated this to JAR creation, since I can run the compiled classes with no issue.

What am I doing wrong when creating the JAR and including the lib/*.jar?

Upvotes: 0

Views: 54

Answers (2)

Troy Neubauer
Troy Neubauer

Reputation: 126

I have had issues with specifying classpath in the manifest file before. If I were you, I would skip referencing the required libraries in the manifest file and instead include them directly inside your jar. Eclipse allows you to easily do this link

Upvotes: 1

GhostCat
GhostCat

Reputation: 140633

You probably assume that the build process should pull the commons JAR into the new that holds your classes. Wrong assumption.

The default Java approach is that you only include your classes in your jar. But instruct your users that they need to have the commons jar in their class path as well!

Upvotes: 0

Related Questions