Neil Turner's Blog

Blogging about technology and randomness since 2002

Java Embedding

Although I haven’t used Java in a while, after seeing the tag soup that Apple recommends for QuickTime movies, I thought I’d look again at how to embed Java code into pages using clean, XHTML-compliant code. It’s possible in Flash so why not Java?
Well, it isn’t possible. You have to use the <object> tag, which IE only has patchy support for. Sun suggests using the following HTML for full browser support:

<object classid="clsid:CAFEEFAC-0015-0000-0000-ABCDEFFEDCBA">
<param name="code" value="Applet1.class">
<comment>
<embed code="Applet1.class" type="application/x-java-applet;jpi-version=1.5.0">
<noembed>No Java Support.</noembed>
</embed>
</comment>
</object>

That’s a lot more complex than it should be, and uses <embed> and <noembed> which are both deprecated in XHTML, as is the Java-specific <applet> tag. The code highlighted in red should tell the browser to use the Java2 1.5 runtime. In theory, we should be able to just use the following code:

<object type="application/x-java-applet" data="Applet1.class" width="300" height="400">
<param name="code" value="Applet1.class" />
No Java Support.
</object>

This works fine in Opera and in Gecko-based browsers, but will display ‘No Java Support’ in IE. For it to work in IE, you need to add the classid attribute so that it can use ActiveX to display the Java code, as follows:

<object type="application/x-java-applet" classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" data="Applet1.class" width="300" height="400">
<param name="code" value="Applet1.class" />
No Java Support.
</object>

That will now work in IE and Opera, but not Gecko – now it will say “No Java Support”. You also cannot add the red code because Gecko will think that Java is not installed.
There’s probably some workaround to this by using the first example and some DOM to add the classid attribute in IE using JavaScript. Either way, I just wish it were simpler, and this is maybe something that both the IE and Mozilla camps need to look into.
incidentally, QuickTime suffers the same problem.

Comments are closed.