Error: java.lang.NoClassDefFoundError: Chase (wrong name: pong/Chase)
I have created an applet program using Eclipse IDE. Now im creating .html file as below:
<html>
<APPLET CODE="Chase.class" width=500 height=400>
</APPLET>
</html>
When Im executing this file the error im getting is:
java.lang.NoClassDefFoundError: Chase (w开发者_如何学JAVArong name: pong/Chase)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
In Eclipse IDE is have created new project and packaged the program into "pong" folder.
Can anybody explain why this error is occuring?
Edit:
Adding few Chase.java code lines, for clarification. It is simple applet:
package pong;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Chase extends Applet implements Runnable
{ ... }
There is no class name Chase
in your classpath.
wrong name
specifies there might be mistake with class name with package specified.
if your class declares package
for example
package a;
public class MyApplet extends Applet{}
then in HTML give a.MyApplet
Update
in your case it seems
<APPLET CODE="pong.Chase.class" width=500 height=400>
also dir structure should be
- - - - -
|
|-your html file
|-pong folder
|
|- Chases.class
will do if package name is pong
I think that the problem is basically as @Jigar Joshi has noted, but with a slight wrinkle to it. I think you have a class whose FQN is "pong.Chase", but you have set up the classpath so that the directory containing "Chase.class" is on the classpath. Then you've told the applet loader to look for a class as "Chase.class".
The classloader has found the bytecode file, but then when it attempted to load it, it has noticed that the classes FQN is "pong.Chase" rather than "Chase" ... as inferred by the name that you gave. Ergo ... a NoClassDefFoundError
, with a message that tells you that the class name is incorrect.
The fix is to make sure that the parent directory of the "pong" directory is on the classpath, and use:
<APPLET CODE="pong.Chase.class" width=500 height=400></APPLET>
Alternatively - use the codeBase
attribute.
Alternatively 2 - get rid of the package
declaration in your Java class.
Alternatively 3 - use the <object>
element. The <applet>
element is deprecated.
Reference: http://www.w3.org/TR/html401/struct/objects.html
You forgot the package part in your applet tag:
<APPLET CODE="pong.Chase.class" width=500 height=400>
</APPLET>
精彩评论