2

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

Rolf Staflin
  • 2,092
  • 2
  • 22
  • 19
  • Use [deployJava.js](http://download.oracle.com/javase/6/docs/technotes/guides/jweb/deployment_advice.html) to deploy the applet, rather than your own home-baked solution. – Andrew Thompson Sep 16 '11 at 02:56
  • I agree. The reason I'm doing it this way is that I'm revising a book about HTML. Toward the end, there is this one example of how to insert a Java applet and I'd like to keep it short. The Java part is way out of scope for the book, but adding an applet to a page isn't. – Rolf Staflin Sep 16 '11 at 06:37
  • Then use the `applet` element, rather than the `object` element. KISS! – Andrew Thompson Sep 16 '11 at 06:40
  • [see my comment on your answer below] – Rolf Staflin Sep 16 '11 at 07:09

2 Answers2

2

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks. Again, I'm updating my book to HTML5. Both the ``tag and the attribute `classid` in `` are gone in HTML5. `` was even deprecated in HTML 4.01 from 1999. I do of course mention these alternatives as fallbacks for older browsers, but I would like to show the proper new way of adding the applet. I can see why the WHATWG have removed the Java-specific stuff from HTML5 and naturally Oracle wants to hang on to the good old days when Java had it's own tag in HTML. Unfortunately that means I have been unable to find any official sources on how to add an applet in HTML5. – Rolf Staflin Sep 16 '11 at 07:07
  • Which brings me back to.. What is wrong with `deployJava.js`? It 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. I feel like we are going around in circles. – Andrew Thompson Sep 16 '11 at 07:10
  • My problem with `deployJava.js` is that it's way over the top for the book. But you are right; it _is_ the correct solution to deploying a Java applet. Please write that again in an answer so I can mark it as the solution! – Rolf Staflin Sep 16 '11 at 07:36
0

Looks like it just doesn't see your applet.

  1. The archive parameter is mandatory The path to your jar has to be specified correctly.
  2. If you don't specify the path, the assumption is it's in the same directory as your testpage, and not a subdir.

And we also assume, that jar contains your Test.class :)

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
andreotti
  • 13
  • 3
  • Thanks for replying! I have checked that carefully, though. It's good to know the archive parameter is mandatory -- where can I find documentation on the parameters? – Rolf Staflin Sep 16 '11 at 06:28
  • *"The archive parameter is mandatory"* No, it is not. Not according to [these examples](http://www.w3.org/TR/WD-object-970218#examples) by the W3C. Further, if the element were an applet element, it would *only* be necessary if the applet uses archives. – Andrew Thompson Sep 16 '11 at 06:44