Header Ads

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 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 loading 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.
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 Settings\anichatt.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


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:



Powered by Blogger.