-1

I get the following exception when i try to run the applet :

load: class MyApplet not found.
java.lang.ClassNotFoundException: MyApplet
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.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)
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 9 more
Exception: java.lang.ClassNotFoundException: MyApplet

applet code :

import javax.swing.*;
import java.awt.*;

public class MyApplet extends JApplet {

public JFrame frame;
public JPanel panel;
public JButton button;

public void init() {
    frame = new JFrame();
    panel = new JPanel();
    button = new JButton("click me ");
    panel.setBackground(Color.RED);
    panel.add(button);
    frame.add(panel);
    frame.setSize(300,300);
    frame.setVisible(true);
}   
}

html code :

<applet code="MyApplet" codebase="AppletPackage" archive="JAR.jar" height="800" width="800">

JAR.jar file contains a package Appletpackage and this package contains a class file named MyApplet.class

enter image description here

Why do i get this exception ? Whare have i made the mistake ?

saplingPro
  • 20,769
  • 53
  • 137
  • 195

2 Answers2

2

The archive parameter is resolved relative to the codebase parameter. So in your case the plugin will look for a file MyApplet.class included in a file AppletPackage/JAR.jar.

You should change this to the following:

<applet code="AppletPackage.MyApplet" archive="JAR.jar" height="800" width="800">

This will resolve to AppletPackage/MyApplet.class inside JAR.jar in the same directory as the HTML file.

MvG
  • 57,380
  • 22
  • 148
  • 276
  • But I get this exception : `java.lang.NoClassDefFoundError: AppletPackage/MyApplet (wrong name: MyApplet)` – saplingPro Jul 09 '12 at 16:01
  • Check whether you can access the jar file using your browser. Make sure the HTML file doesn't contain a `` setting. The edit the location bar of your browser. replacing the part after the last `/` with `JAR.jar`. If that works, use e.g. WinRar to check that there is a file `MyApplet.class` inside a `AppletPackage` directory within that jar. I very much expect either of these to fail. – MvG Jul 09 '12 at 16:08
  • JAR.jar is accessible _http://localhost:8084/poll/JAR.jar_. poll is the name of the project . AppletPackage contains MyApplet.class – saplingPro Jul 09 '12 at 16:13
  • are you able to do the same on your pc but inside a netbeans project – saplingPro Jul 09 '12 at 16:19
  • I don't use netbeans. But as the problem lies on the client side of the HTTP connection, its server side should be irrelevant. It should be possible to simply save the HTML to a file, drop that into the same directory as JAR.jar, and then open that file in a browser. It might be that the Java plugin prevents an applet from creating a frame, but in that case I'd expect a different exception. – MvG Jul 09 '12 at 16:26
  • thank you ! But can you please explain me the `codebase` attribute. What is it meant for ? – saplingPro Jul 10 '12 at 07:26
  • 1
    @grassPro, in my understanding `codebase` is mainly intended for the case where you don't use a jar but instead have all the class files under some directory. In that case you'd have `codebase` refer to that directory, where the plugin will find subdirectories for packages and within those individual files for classes. As this requires a distinct HTTP GET request for each class, the use of jar files is more efficient under pretty much all circumstances, even without considering the compression of jar files. I wouldn't use `codebase` together with `archive`. – MvG Jul 10 '12 at 08:13
1

This is an attempt to address the error message reported in a comment to my first answer:

java.lang.NoClassDefFoundError: AppletPackage/MyApplet (wrong name: MyApplet)

Looking at the sources, I see that this “wrong name” error message is an indication of a mismatch between file name and class name. You claim that your class is inside AppletPackage, and the file name AppletPackage/MyApplet.class fits that. But the source code you quoted above didn't contain a line

package AppletPackage;

You should add that line, so that the class file contains the fully qualified name of the class. Then you should be able to load it.

MvG
  • 57,380
  • 22
  • 148
  • 276