6

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 (wrong 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
{ ...  }
Gaurav Navgire
  • 780
  • 5
  • 17
  • 29

3 Answers3

8

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

jmj
  • 237,923
  • 42
  • 401
  • 438
  • Tried to use the help. The error still somes as:oad: class pong/Chase.class not found. java.lang.ClassNotFoundException: pong.Chase.class 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)....etc – Gaurav Navgire Jan 24 '11 at 09:43
  • Yes the package name is "pong" and after doing the above steps it works, when executed independently. However when i have to add a custom html code into a custom webpage of a website then the error is same as above. – Gaurav Navgire Jan 24 '11 at 10:19
  • `when i have to add a custom html code into a custom webpage of a website` please explain – jmj Jan 24 '11 at 10:20
  • widget based website creators provide a widget where in we can drag a HTML Code widget into our webpage and add a html code snippet into it.This is where i want to add the applet code. So that the applet is excutable on the page. – Gaurav Navgire Jan 24 '11 at 10:40
  • ok then you also need to provide `class` in the same structure shown above in that server in order to load applet – jmj Jan 24 '11 at 10:41
2

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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • the alternative 2 works no doubt. i have to add this applet code snippet into a webpage. When added, the error is same as earlier. But when the html file is executed independently is works. Why is it so? – Gaurav Navgire Jan 24 '11 at 10:21
  • @GamDroid - I suspect it is the directory structure; see @Jigar Joshi's updated answer. – Stephen C Jan 24 '11 at 13:38
0

You forgot the package part in your applet tag:

<APPLET CODE="pong.Chase.class" width=500 height=400>
</APPLET>
dogbane
  • 266,786
  • 75
  • 396
  • 414