Class NoClassDefFoundError in Java

Class NoClassDefFoundError in Java

NoClassDefFoundError is thrown from an application if the Java virtual machine or a class loader tries to load the definition of a class(as part of the normal method call or as part of creating a new instance using the new operator) and no definition could be found in the path.

In short, a NoClassDefFoundError is thrown when the definition of a class cannot be found.

Ads code goes here

The structure of the class NoClassDefFoundError  is given as:

public class java.lang.NoClassDefFoundError extends java.lang.LinkageError{
//constructor
public NoClassDefFoundError();
//constructs an empty NoClassDefFoundError object that is an object with no message specified.
public NoClassDefFoundError(String message);
//constructs an NoClassDefFoundError object with the message specified.
}

The class NoClassDefFoundError also inherits methods from class Object and Throwable.

From Object classFrom Throwable class
clone()fillInStackTrace()
getClass()getMessage()
notify()printStackTrace()
wait()printStackTrace(PrintWriter)
wait(long, int)getLocalizedMessage()
wait(long)printStackTrace(PrintStream)
equals(Object)
toString()
finalize()
hashCode()
notifyAll()

How To Solve NoClassDefFoundError In Java?

How To Solve NoClassDefFoundError In Java
How To Solve NoClassDefFoundError In Java

If you are new to java, trying to write Applet code, this error might come, while you try to run Applet code in the browser.

What is the problem actually:
java.lang.NoClassDefFoundError This exception indicates that the JVM looked in its internal class definition data structure for the definition of a class and did not find it. This is different than saying that it could not be loaded from the classpath.

Usually, this indicates that we previously attempted to load a class from the classpath, but it failed for some reason – now we’re trying again, but we’re not even going to try to load it, because we failed to load it earlier.
The earlier failure could be a ClassNotFoundException or an ExceptionInInitializerError (indicating a failure in the static initialization block) or any number of other problems. The point is, a NoClassDefFoundError is not necessarily a classpath problem.

READ  ClassFormatError in Java

Let me show you what kind of error it throws..

Java Plug-in 10.9.2.05
Using JRE version 1.7.0_09-b05 Java HotSpot(TM) Client VM
User home directory = C:Documents and Settingsanichatt.APPLICATIONS
----------------------------------------------------
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
l:   dump classloader list
m:   print memory usage
o:   trigger logging
q:   hide console
r:   reload policy configuration
s:   dump system and deployment properties
t:   dump thread list
v:   dump thread stack
x:   clear classloader cache
0-5: set trace level to 
----------------------------------------------------

Here is a step by step guide how to stop this error.
1. Same directory: If you are new try to write the source file(say xxx.java) in the bin of the java. This will create the class file (xxx.class) in the same directory. It will help you to start with.
2. Check if you are using latest java edition. There should not be two versions of java…an old one and a new one.
3. If you are using some package correct your HTML applet code it should look like-

<applet archive="some.jar" code="some_pkg.xxx" height="600" width="440">
</applet>

4.There should not be a missing jar. for multiple jar use the below written format..

<applet archive="some.jar,Some2.jar..." code="some_pkg.xxx" height="600" width="440">
</applet>

5. If you are using some advanced editor like Eclipse or JDeveloper, it will automatically create some folder strucure.
Say you have a structure like-
package-pck1.
inside that you have all your .class file and HTML
then applet code will look like-

<applet code="pck1.xxx.class" height="400" width="500"></applet>

also you can add codeBase attribute for better support.it is advisable not to use package deceleration when using Applet.

6. Check if you are using HTML version-5 for running Applet code.if you are using so, please note Applet element is deprecated now. Please use Object instead of Applet.
New structure will be-

<OBJECT codetype="application/java"
        classid="java:xxx.class"
        width="500" height="500">
Some thing
</OBJECT>

7. There may be situation when you use an advanced editor like net-beans or eclipse or JDevelper Applet is working fine but when you try to export to jar..you might encounter this issue–
in such case put the jar to /webapps/myapp/WEB-INF/lib directory

READ  Class BufferedOutputStream in Java

8. Please dont forget to add .class extension to the file you are trying to embed. Some times we forget this simple step.

<applet code="xxx.class" codebase="somedir" ... ></applet>

9. Please make sure that the jar file is in correct classpath if at all you are using jar functionality.
10.If you are using third party library please don’t forget to add this library as a jar to the applet jar.For this you must manually modify the jar manifest.
11. To work with external data source like-notepad,CSV or excel, please try to put in the same folder/directory or embed the same.
12. If you are mixing applet and web coding be careful here.Try to use appframework jar.Also applet requires some other folder as applet’s jar.
13.Java is very much case sensitive. Be careful while providing any info to java. They should spelled correctly with respect to your structure.
14. Sometimes it may happen that this error is coming sometimes but otherwise working fine. Well if there is no free space to server this problem may occur  Try to free up some space and try to re run.

Bugs to refer:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6888528
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7169161

 

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *