Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception in thread "main" java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant

Tags:

java

jar

jodatime

I built an executable jar using an ant script, the only external jar I used was joda-time 2.0. The ant build script "seemed" to work as I did not recieve any compile errors, and if I were to remove the jode-time 2.0.jar from the lib directory the build would indeed fail as expected. At any rate, after building the jar I get this error when I try to run with:

java -jar myapp.jar

Exception in thread "main" java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant

Any thoughts on this? I'm just not sure where to look, everything works just fine within Eclipse. thanks for any ideas

like image 507
RandomUser Avatar asked Aug 23 '11 21:08

RandomUser


People also ask

How to fix java lang NoClassDefFoundError in java?

lang. NoClassDefFoundError, which means the Class Loader file responsible for dynamically loading classes can not find the . class file. So to remove this error, you should set your classpath to the location where your Class Loader is present.

How to handle NoClassDefFoundError in java?

You can fix NoClassDefFoundError error by checking following: Check the exception stack trace to know exactly which class throw the error and which is the class not found by java.

Why I am getting NoClassDefFoundError?

The NoClassDefFoundError is a runtime error in Java that occurs if the Java Virtual Machine (JVM) or a ClassLoader instance attempts to load the definition of a class that could not be found. The class definition exists at compile-time but is not available at runtime.

What is Joda time?

Joda-Time is the most widely used date and time processing library, before the release of Java 8. Its purpose was to offer an intuitive API for processing date and time and also address the design issues that existed in the Java Date/Time API.


2 Answers

Presumably, your jar doesn't contain a manifest header telling Java to add the joda-time jar to the classpath. That's the only way to have other classpath entries when using java -jar. You could do this directly with the Ant manifest task, or there are probably multiple other ways to do it, including building it from your existing classpath.

Alternately, try

java -cp myapp.jar:joda-time-2.0.jar com.foo.YourMainClass
like image 54
Ryan Stewart Avatar answered Nov 02 '22 22:11

Ryan Stewart


Unless you're using a custom classloader or something like JarJar, then you cannot bundle external JARs inside your executable JAR. Your manifest file will need to list a classpath, but the JVM will look for the JARs you list in the same directory as your executable JAR, not inside your executable JAR.

like image 24
dty Avatar answered Nov 02 '22 23:11

dty