<< The C++0x Working Draft: Get It Now | Home | Friday Java Quiz: Now For A Real Test >>

This Applet Not Brought To You By...

... a buggy JavaScript file.

([Note] The following would have been a charming little blog entry with a small Java applet that warms people's hearts, had I not ran into some troubles that made the whole thing, eh, what shall I say, a little annoying.)

Along with Java 6 Update 10, released 14 days ago, comes a JavaScript file deployJava.js hosted at http://java.sun.com, that, among other things, helps in deploying Java applets.

Here's a little applet, deployed according to this advice:

(Source of a would-be applet follows)

<div style="margin-left:3em">
<script src="http://java.com/js/deployJava.js"></script>
<script>
    var attributes =
        {id:'squareApplet',
        codebase:'http://www.weiqigao.com/blog/files',
        code:'com.weiqigao.square.SquareApplet.class',
        archive:'square.jar',
        width: 100, height:100};
    var parameters = {color: 'red'};
    var version = '1.6' ;
    deployJava.runApplet(attributes, parameters, version);
</script></applet>
</div>

<script>
  function toBlue() {
    document.squareApplet.toBlue();
  }

  function toRed() {
    document.squareApplet.toRed();
  }
</script>

<p>And you can change its color to <a href=  "javascript:toBlue()"
>blue</a> and <a href= "javascript:toRed()" >back</a>.</p>

Here's the issues I had with this setup:

  • I don't have Java plug-in on my 64-bit Debian GNU/Linux machine. This causes the above script to auto-redirect to Java Downloads for Linux. But it forgot one thing: there isn't a Java plug-in for 64-bit Linux to download. In the mean time, I can't even go back to the blog entry to edit it.
  • So I switched to the Windows machine to finish the editing. For some reason, the tags output by the JavaScript is missing the closing applet tag, which, in Firefox or IE7 causes the Save button to not show up.
  • Luckily, Google Chrome was more tolerant, and I was able to resume my editing. I added the closing </applet> tag after the JavaScript code to make it work in Firefox 3 and IE7.
  • One thing I want to experiment with is the revived LiveConnect. But apparently it works only if the applet extends java.applet.Applet. I extended javax.swing.JApplet initially, thinking that JApplet is applet too. But none of the Java methods on it was callable. As soon as I changed to java.applet.Applet, Java method calls from JavaScript worked.
  • They worked in Firefox 3 and IE7, but not Google Chrome nor Safari. I guess such is web programming.
  • After all these fuss and trials and errors, once things worked as documented, it did feel fast and snappy. The start up delay is not longer than that of a Flash animation or even AJAX controls. Sun really did eliminate the Java/Applet start up disadvantage.
  • As I modified the applet and jarred it up and uploaded it to the server, I have to fight the JRE cache on the browser side to load the new version of the jar file. At one point, I removed the jar file from the server, restarted Tomcat, yet the browser still had the applet running, even after clicking on the reload button multiple times while the shift or control key was held down.

For the curious, here is the happy version of this blog. (Beware of Applet! You've been warned.)

Tags :


Re: This Applet Not Brought To You By...

I use JApplet and JavaScript just fine. The only thing is: you have to wait until the applet is fully loaded before you can call into it. Checking for the presence of certain methods in the applet does not work, I've reported this as a bug to SUN (but not fixed for now).

Re: This Applet Not Brought To You By...

I haven't done exhaustive research on this. What I did might be called a form of programming by accident: I changed from JApplet to Applet, and my page worked.

Re: This Applet Not Brought To You By...

"I added the closing </applet> tag after the JavaScript code to make it work in Firefox 3 and IE7." Myself I would be more comfortable with this markup if there was a opening tag for the <applet> tag also. I think IE6 might just go into quirks mode because of something like this. It just can add some extra headache is all I'm thinking.

Re: This Applet Not Brought To You By...

Strange thing is, in line 335 of the deployJava.js, the last statement of the writeAppletTag function definitely seems to write the closing applet tag.. It's revision header is 1.13 08/10/28 -- did they perhaps update the script?

Re: This Applet Not Brought To You By...

I did not look at the source of deployJava.js at the time when I did the original experiment, which was 2008/10/27 by the way.

I just removed my bogus </applet> tag on the happy applet page and everything displayed fine. So it would seem that the problem is fixed.


Add a comment Send a TrackBack