Ben
Ben

Reputation: 11

Embedding an applet doesn't work on my website

I'm trying to code an applet and put it in my website. I remember doing this a long time ago using Borland back when 1.4 was the latest version. It of course used the applet tag (which I'm using currently) and it had no issues. But anyways, I put the class files in httpdocs/ under its own directory, and then used this code in the web page:

<applet code="wsavatar/WSAvatar" width="425" height="150> Your browser does not support the applet tag. </applet>

And when I try to load the page, this happens:

Java Plug-in 1.6.0_17 Using JRE version 1.6.0_17-b04 Java HotSpot(TM) Client VM

java.lang.ClassFormatError: Incompatible magic value 1008813135 in class file   
  wsavatar/WSAvatar
  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)
Exception: java.lang.ClassFormatError: Incompatible magic value 1008813135
in class file wsavatar/WSAvatar

I have tried making a quick local html file to load the applet using the same applet code and it worked. I have looked around online and have heard various things pertaining to this error, but nothing seems to alleviate it, that I've found. Any ideas?

Ben

Upvotes: 1

Views: 1855

Answers (2)

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

The magic number is a four byte value (0xCAFEBABE) at the start of the class file that marks it as a class file rather than any other kind of data.

So what type of file has magic number 1008813135? In hex that becomes that bytes 0x3C, 0x21, 0x44, 0x4F. Interpreted as character data in common Latin encodings is "<!DO. Probably continuing as "<!DOCTYPE". What we have here is an HTML file. This is probably an error page returned by a broken server only with a non-error success code in the HTTP response.

Have a look at what is actually being served. A web browser will probably show the page. It is worth learning to use telnet (or nc) and type in the HTTP response by hand. There are also various utilities for inspecting HTTP traffic.

Upvotes: 5

William Billingsley
William Billingsley

Reputation: 771

The problem appears to be in your website, not your code. Googling around, the error seems to be most frequently reported with systems such as OpenCMS (especially for URLs that are on port 8080).

So, what I imagine is happening is that the browser's call to load the applet is actually failing, but is returning data rather than a 404/500 error. The Java VM is attempting to interpret the returned error page as if it was a class file, and quite correctly complaining that it doesn't seem to be a valid class file after all. (This happens quite often with content management systems that redirect to the home page rather than return an actual HTTP error to the user.)

To test, try manually typing the URL to the applet (not the page it is hosted in, the applet itself) in your browser and see what the server returns.

Upvotes: 3

Related Questions