Monday, August 31, 2009

Serving up applets on Pluto

The problem is, if I write a simple html page with a simple applet I can display it in apache2 no problem:

import java.awt.Graphics;
public class HelloWorld extends java.applet.Applet {
    public void init() {
 resize(150,25);
    }
    public void paint(Graphics g) {
 g.drawString("Hello world!", 50, 25);
    }
}

And the view.html is:

<html>
<body>
<applet code="HelloWorld" width="450" height="250">
</applet>
</body>
</html>

Now that works, but if you put this into Pluto and serve the page up, with the HelloWorld.class in the same folder as the view.html file you get this error:

java.lang.ClassFormatError: Incompatible magic value 168430090 in class file HelloWorld
 at java.lang.ClassLoader.defineClass1(Native Method)
 at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
 at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
 at sun.plugin2.applet.Applet2ClassLoader.findClass(Applet2ClassLoader.java:140)

which tells me that the class loader tried to load HelloWorld.class and it received a magic hex number of 0A0A0A0A, not CAFEBABE which begins every class file in Java. And the reason is that it received a 404 not found message from the tomcat server, and presumably the 404 HTTP header begins with 4 new line characters.

So there are two possible reasons:

  1. The HelloWorld.class file should be somewhere else
  2. The HelloWorld file needs to be served manually by Tomcat, in the same way that image files are served. But this should happen by default, so I'm going to try option 1 first.

It turns out that the hello world example (once I removed the ".class" from the name) could be served if I specified the full web url: /monitor/jsp as the codebase of the applet. Doh! Another Homer Simpson moment. Time for a slice of delicious Floor-Pie.

OK, I applied the above strategy to the monitor applet and it worked. Only I had to also specify the archive attribute as applet.jar. Now to add an edit page to the portlet so I can change the parameters of the applet.

No comments:

Post a Comment