ClassNotFound exception when loading applet in Chrome

Rolf Staflin picture Rolf Staflin · Sep 15, 2011 · Viewed 12.1k times · Source

I'm having a hard time getting a Java applet to run in Chrome. The class loader can't find the class, even though the it works fine in Firefox, Opera and Safari.

Here's my test applet class (I even took out the package declaration to keep it simple):

import java.awt.Graphics;
import java.applet.Applet;

public class Test extends Applet
{
    public void init() { repaint(); }

    public void paint( Graphics g ) {
        g.drawOval(10, 10, 30, 50);
        g.drawLine(15, 30, 22, 32);
        g.fillOval(28, 28, 7, 5);
        g.drawArc(15, 20, 20, 35, 210, 120);
    }
}

Here's the minimalistic test page:

<!doctype html>
<html><head>
  <meta charset="utf-8"/>
  <title>Test</title>
</head><body>
  <p>
    <object type="application/x-java-applet" width="50" height="70">
      <param name="code" value="Test" />
      Test failed.
    </object>
  </p>
</body></html>

Here's the stack trace:

java.lang.ClassNotFoundException: Test
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Applet2ClassLoader.java:252)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Plugin2ClassLoader.java:250)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:180)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:161)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Plugin2ClassLoader.java:687)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Plugin2Manager.java:3046)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1498)
    at java.lang.Thread.run(Thread.java:680)

I've compiled the class with javac Test.java, and I put the .class file in the same folder as the .html file. Again, this runs fine in both Firefox, Safari and Opera, so why not in Chrome?

I've tried creating a jar and adding <param name="archive" value="Test.jar" />, but that didn't help.

Oh, and while I'm asking stuff: Is there an official spec listing the <param> parameters one can use with applets in <object> tags? It's not in the HTML5 spec, which makes sense, but Oracle seems to favor the old abandoned <applet> tag and I need to use strict HTML5.

Environment

MacBook Pro running OS X 10.7.1

java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03-383-11A511)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-383, mixed mode)

Google Chrome 13.0.782.220

Answer

Andrew Thompson picture Andrew Thompson · Sep 16, 2011

To keep it simple for testing, change this:

<!doctype html>
<html><head>
  <meta charset="utf-8"/>
  <title>Test</title>
</head><body>
  <p>
    <object type="application/x-java-applet" width="50" height="70">
      <param name="code" value="Test" />
      Test failed.
    </object>
  </p>
</body></html>

To this:

<html>
<head>
  <meta charset="utf-8"/>
  <title>Test</title>
</head>
<body>
  <p>
    <applet code="Test" width="50" height="70">
      Test failed.
    </applet>
  </p>
</body>
</html>

Update 1

Note that deployJava.js is the correct way to embed an applet these days. It writes an object or embed element as the browser needs it, and it aims to do it correctly.