Jun 25, 2013

f Comment

[SOLVED] Fixing Java No Class Def Found Error and Class Not Found Exception in MINUTES!

Amazon java.lang.NoClassDefFoundError is one of the most evil errors you can run into while running a Java program.

The error looks something like the following.
$ java BlogAutomate -action check
java.lang.NoClassDefFoundError: com/google/gdata/data/TextConstruct
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.google.gdata.data.TextConstruct
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 6 more
Exception in thread "main"
Let's look at the cause of this error and how we can solve the error and exception easily!

Root Cause
The cause of this error is simply that while running a Java program, the JVM cannot find the needed class file which contains the definition of a class your code is using.

For example my Java program is using com.google.gdata.data.TextConstruct but I don't have the corresponding class file or jar where JVM can find it. Therefore I get java.lang.NoClassDefFoundError caused by java.lang.ClassNotFoundException when I run the program.

Solution
If you've read my post on How Does Java Know Where to Find Core Libraries? you'll know why Java cannot find the class files.

In my case I was using JDK 1.6. This means my ext/ folder is at C:\Program Files (x86)\Java\jdk1.6.0_23\jre\lib\ext and my environment PATH points at C:\Program Files (x86)\Java\jdk1.6.0_23\bin to run a Java program.

I am using Google's API for Blogger and that's why I have many Google jars in C:\Program Files (x86)\Java\jdk1.6.0_23\jre\lib\ext such as gdata-webmastertools-2.0.jar and gdata-maps-2.0.jar.

One day Firefox prompts me to upgrade to the latest JDK while viewing a webpage with embedded applets such as my post on Comprehensive Guide on Solving the Rubik's Cube!. When I finished upgrading my environment might have been changed to point to the latest JDK.

To find out which version of Java you are running simply run 'java -version' in the terminal.
Therefore simply copy the jars from old ext/ to the new ext/. In my scenario I copied all the jars from C:\Program Files (x86)\Java\jdk1.6.0_23\jre\lib\ext to C:\Program Files (x86)\Java\jre7\lib\ext and my problem is solved!

While copying the jars from old ext/ to new ext/ folder do NOT overwrite the jars with identical names because your latest SDK needs to their original jars to work.

Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael