Launching Applets


It used to be simple to put an applet in a web page where it would be used by Netscape browsers, Internet Explorer and others. No more.

There's a war on between Sun and Microsoft. Sun wants to promote its platform independent java language and Microsoft wants to extend its Windows monopoly. That monopoly is threatened by, among other things, applets which promise to make the underlying operating system irrelevant. (Here's Microsoft's position. It wants its customers to abandon java by 12/31/2007.)

Who gets hurt in a war like this? That would be you and me - uninvolved bystanders who just want to use this technology. For example, I'd like you to take a look at my ColorChooser. In print I'd show you a screen shot, like this one:

Screen shot of the author's color chooser replacement for the Swing ColorChooser

But what I'd really like to do on the web is let you actually use the tool. Then you can press the buttons, slide the slider and click on a color. The button below this paragraph is a sample of what I really want - go ahead and launch the ColorChooser and fiddle with it to see how it works. (Try clicking on a color you like in the big color wash while you have green scrolling.)

(From the navigation bar on the left, click ColorChooser to see mine compared to Sun's.) That sure beats a screen shot, doesn't it? That's what you should be able to do on the web. But is it easy?

Actually, writing the ColorChooser took just one day. Getting it on the web took nearly a month! Here's why.

Applets and JVMs

First there's the java virtual machine. The JVM is a computer simulated in software. Java applets are distributed in .class files which contain a pseudo-machine language that runs in a JVM. The original java-enabled browsers included a JVM licensed from Sun. That was back in the days when Netscape was the dominant browser and Internet Explorer was a newcomer that had to match Netscape's features.

Most contemporary browsers come bundled with Sun's latest JVM, with one exception: Internet Explorer. Of course, IE is now the dominant browser but it is sadly out of date. It still comes bundled with 1997's JVM 1.1. Here's the history:

  1. java 1.0 - 1996
  2. java 1.1 - 1997
  3. java 1.2 - 1998 (aka java 2)
  4. java 1.3 - 2000
  5. java 1.4 - 2002
  6. java 5.0 - 2004 (aka java 1.5)
So if you want to run applets in todays browsers without any extra work, you have to generate java 1.1. (Pop quiz, java developers: can you generate java 1.1 without JDK 1.1?) That means no Graphics2D, no Swing . . . nothing up to date. But there's an alternative.

The Java Plug-in

When Microsoft refused to upgrade its JVM, Sun created the Java plug-in, also called the Java Runtime Engine or JRE. With any browser plus the JRE, the user can run applets with all the features of the latest java versions. If your applet needs java 1.4 features, all that you need is a browser plus the JRE v. 1.4.

To make matters easier, it is possible to automate the process. My applets here, for example, check that your browser has access to a JRE v. 1.4 or later. If you have a contemporary browser (Firefox or Opera) you will already have a current JRE. If you've run any other applet that required a modern JRE you'll also have it. If not, the process of getting and installing the current JRE is made just as simple as possible. On Internet Explorer, the whole installation is automatic.

But that automation is not without a price.

Applet and Other JavaScript Tags

In the good old days when the browser market was competitive, the webmaster who wanted an applet named "MyApplet" to run added an applet tag to the page, where the applet was wanted. That could be as simple as this:


	<APPLET NAME="MyApplet" WIDTH=300 HEIGHT=200>
	If you see this message, your browser cannot run MyApplet.
	</APPLET>

This tag knows nothing about JVM versions, of course. To get the desired effect - check that the user has JVM 1.4 or later, download a new JVM if needed, then run MyApplet - this code is required on Internet Explorer.


	<OBJECT "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
		CODEBASE="http://java.sun.com/products/plugin/" + 
			"autodl/jinstall-1_4-windows-i586.cab#" +
			"Version=1,4,0,0"
		WIDTH=300 HEIGHT=200
		PARAM NAME=CODE VALUE="MyApplet"
		PARAM NAME=TYPE VALUE=
			"application/x-java-applet;jpi-version=1.4"
	If you see this message, your browser cannot run MyApplet.
	</OBJECT>
That code works for Internet Explorer. Suppose you wanted to support Netscape (and Firefox, Opera and other browsers) you can't use an OBJECT tag. You have to use the industry-standard (where "industry" does not include Microsoft) EMBED tag, this way:

	<EMBED
		TYPE="application/x-java-applet;version=1.4"
		CODE="MyApplet"
		WIDTH=300 HEIGHT=200
		PLUGINSPAGE="http://java.sun.com/products/plugin/" + 
			"index.html#download"
		If you see this message, your browser cannot run MyApplet.
	</EMBED>

I'm simplifying here. For more complete details, take a look at Sun's tag documentation: As hefty as Sun's docs are, they are only single chapters. There are 28 chapters and six appendices in the 1.4 plug-in documentation. Worse, they're still incomplete. You read that the "code" parameter's value should be the name of your applet. OK, is that "MyApplet" or is it "MyApplet.class"? Suppose MyApplet.class is not in the default package (it shouldn't be in the default package)? And if you discover those answers testing in your browser, what about in other browsers?

This is the answer to my sample question. The "code" value is actually the fully-qualified name of your applet. If MyApplet is in the "applets" package, the value should be "applets.MyApplet". It should not include the ".class" as "MyApplet.class" refers to the "class.class" file in the "MyApplet" package. (Well, that's the theory but there are exceptions.)

Automated Tag Combining

You can combine OBJECT and EMBED tags into this even-less-readable structure that has the advantage that it will run in either an IE or a Netscape-class browser.


	<OBJECT "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
		CODEBASE="http://java.sun.com/products/plugin/" + 
			"autodl/jinstall-1_4-windows-i586.cab#" +
			"Version=1,4,0,0"
		WIDTH=300 HEIGHT=200
		PARAM NAME=CODE VALUE="MyApplet"
		PARAM NAME=TYPE VALUE=
			"application/x-java-applet;jpi-version=1.4"
		<COMMENT>
		<EMBED
			TYPE="application/x-java-applet;version=1.4"
			CODE="MyApplet"
			WIDTH=300 HEIGHT=200
			PLUGINSPAGE="http://java.sun.com/products/plugin/" + 
				"index.html#download"
			If you see this message, your browser cannot run MyApplet.
		</EMBED>
		</COMMENT>
	
	</OBJECT>

To make things simpler, Sun has an HTMLConverter program that lets the webmaster create the original APPLET tag and then automatically generates the OBJECT/EMBED combination tag. Note that I said "simpler" and not "simple." If you're from the "show me" school, here is Sun's HTMLConverter documentation.

If you read the HTMLConverter documentation you may have noticed that there is an "extend.tpl" file - an extended template. The tagging above works for PCs, but suppose you have a Macintosh or a Solaris workstation? What happened to Write Once, Run Anywhere?

Enter the JavaScript Solution

Remember that ColorChooser applet at the start of this discussion? I launched that by inserting a bit of javascript (the browser scripting language that is definitely not to be confused with the java language). This call to a launch function is all I needed:


	<SCRIPT TYPE="text/javascript">
		launchApplet( "MRCCdemo", 200, 50 );
	</SCRIPT>
Now you can see what you probably wanted all along. You give the name of the applet, width and height and let the computer do the work. What were all those complications?

Actually, all those complications are still there but they've been buried in a javascript library that includes the launchApplet() method. If you looked into that method you'd see it ask a lot of questions, such as, "Is the text 'Macintosh' included in the browser name? IF it were included it would call a method named macintosh(). The macintosh() method will write the code needed for a Macintosh computer into the HTML page as the browser loads the page.

That javascript library code is subject for another article.

Summary

Applets burst on the scene in 1996 and 1997. They were mostly used for eye candy - animations and other fun effects. Today they are highly useful as a way of providing remote access to programs to a general audience.

Unfortunately, the Sun v. Microsoft war has made it very difficult to launch an applet. The problem has solutions but you'll need some serious determination to wade through the issues that have been introduced here.


Pop quiz answer! Use the "target" switch. (No, I didn't know this until I got deeply into this applet issue.)


	javac -target 1.1 [more switches] YourClass.java

© 2005 by Martin Rinehart


java consultant home page icon

Back to Articles