Lesson 14.6 Java Plugin & Converter
Wouldn't it be nice if all your Java applets could work together with any browser that you, or your users, chose to use? Well, even though that day hasn't completely arrived yet, it is a little closer, thanks to Sun's Java Plug-in, which you can see here, happily running the Java 2D demonstration applet in Internet Explorer.

Whenever you install Sun's Java 2 Software Development Kit, Version 1.3, or the freely distributable Java Runtime Environment (JRE), the Java Plug-in is automatically installed, which allows your Java 2 applets to work in any version of Netscape Navigator or Internet Explorer, without any extra work on your part.
Well..., almost no work. You see, for your applets to use the Java 2 Plug-in, instead of the native Java support provided by your Web browser, the HTML page that hosts your applet must be changed so that all <APPLET> tags use <OBJECT> or <EMBED>, depending on the browser.
If you know which browser your viewers are using, changing your tags is not that much work. To support both IE and Navigator, however, the necessary HTML and JavaScript "browser sniffing" code can be quite complex. Here, for instance, is the code that is required to display a simple "HelloWorld" applet using the Java Plugin:
<!-- The following code is specified at the
beginning of the <BODY> tag. -->
<SCRIPT LANGUAGE="JavaScript"><!--
var _info = navigator.userAgent; var _ns = false;
var _ie = (_info.indexOf("MSIE") > 0 &&
_info.indexOf("Win") > 0 &&
_info.indexOf("Windows 3.1") < 0);
//--></SCRIPT>
<COMMENT><SCRIPT LANGUAGE="JavaScript1.1"><!--
var _ns = (navigator.appName.indexOf("Netscape") >= 0
&& ((_info.indexOf("Win") > 0
&& _info.indexOf("Win16") < 0
&& java.lang.System.getProperty("os.version").
indexOf("3.5") < 0)
|| _info.indexOf("Sun") > 0));
//--></SCRIPT></COMMENT>
<!-- The following code is repeated for each APPLET tag -->
<SCRIPT LANGUAGE="JavaScript"><!-- if (_ie == true) document.writeln(' <OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="200" height="200" align="baseline" codebase="http://somewhere.com/win.cab#Version,2,2,0"> <NOEMBED><XMP>'); else if (_ns == true) document.writeln(' <EMBED type="application/x-java-applet;version.2.2" width="200" height="200" code="HelloWorld.class" pluginspage="http://somewhere.com/plugin-install.html"> <NOEMBED><XMP>'); //--></SCRIPT> <APPLET code="HelloWorld.class" width="200" height="200"> </XMP> <PARAM NAME="java_code" VALUE="HelloWorld.class"> <PARAM NAME="java_type" VALUE="application/x-java-applet;version.2.2"> <PARAM NAME="scriptable" VALUE="true"> No JDK 1.2 support for APPLET!! </APPLET></NOEMBED></EMBED> </OBJECT> <!--
<APPLET code="HelloWorld.class"width="200" height="200">
No JDK 1.2 support for APPLET!!
</APPLET>
--> |
Don't despair just yet, however. Sun has provided a tool to make this HTML conversion automatic.
|
Java Plugin News...
|
| A lot of this material is already out of date, because the latest, early-release version of the Java plugin no longer require the strange HTML to use the plugin; instead, you can configure the plugin to recognize applet tags in IE or Netscape 6. Even with the latest plugin, however, you still need to use the HTML converter to make Netscape Communicator use the plugin instead of its built-in version of Java. |
The HTML Converter
Sun's HTML converter is a free tool that automates the process of converting old-style <APPLET> tags to the <OBJECT> or <EMBED> tags required to launch the Java Plugin.
Here is an overview of the steps you need to follow to use the new tool:
- Start the converter.
- Select the files to process.
- Specify a backup directory for your unconverted files.
- Specify the conversion type.
- Convert your files.
- Test the results.
- Optional: finding help and setting advanced options.
Here are step-by-step instructions that will help you get started with the HTML converter.
Step 1 - Start the Converter
Since version 1.3 of the Java Software Development Kit, the HTML converter has automatically been downloaded and installed in the JDK lib directory. The converter is a Java 2 application, it must be started from a DOS console or shell window, and the Java 2 environment must be properly initialized.
Once you've opened a console window, start the HTML Converter program by first switching to the JDK lib directory, and typing java -jar htmlconverter.jar -gui to launch the GUI version of the program. On my XP system, here are the commands I typed:
C:\> CD \jdk1.3.1_01\lib
C:\jdk1.3.1_01\lib> java -jar htmlconverter.jar -gui |
When the HTMLConverter application starts up, you'll see a screen that looks similar to this:

Step 2 - Select Files
As you can see, the converter has radio buttons that allow you to work on all the files in a folder, or on a single file at a time. If you choose "All Files in Folder", you can also choose whether to automatically convert Subfolders and choose the type of files to convert.
To convert all of the files in the JavaProjects directory, click on the Browse button and navigate to the folder as shown here.
Note that the dialog that appears is only used for selecting folders, not for selecting files.
Step 3 - Specify Backup Directory
Fill in the "Backup Files to Folder" text field by browsing to an existing directory or simply typing in the name of the directory you want to use. If the directory does not already exist, it will be created.
Remember, the Java HTMLConverter converts its HTML files "in place." Always make sure you've backed up your files by filling in an appropriate value for this field.
Step 4 - Specify Conversion Type
As you can see from the screen shot below, by default HTMLConverter creates a file that works for both Internet Explorer and for Netscape Navigator. You may, however, choose between any of the options you see here:
Of course, having so many choices doesn't help much if you don't know what they do! Here's a short run-down:
- Standard writes code that uses JavaScript to dynamically determine which Web browser is running and then invokes the appropriate code as necessary.
- Extended does the same thing as Standard, but also writes a regular <APPLET> tag in the event that the Web browser is running on an OS other than Windows or Solaris, or if the browser is neither Navigator nor IE. This is useful if your viewers will be using another Web browser--such as Opera--which can launch the Java Plug-in using regular <APPLET> tags. It doesn't help, however, if the browser is incapable of running Java 2 applets.
- The IE and Navigator only options operate as you'd expect.
- The Other Template option allows you to install custom templates and select them from a dialog box.
Step 5 - Convert Your Files
Once you click the Convert button, conversion is automatic. When finished, you'll be shown a dialog box like this:
Step 6 - Test the Results
If you choose to do a Standard conversion, make sure your applets run--at least--in both IE and Navigator. It also wouldn't hurt to try a few different versions of each browser as well, such as the 3.0 versions of both Navigator and IE.
Here you can see the FontMaster applet running under IE-4, using the Java Plug-in.
One potential complication is that applets launched using the Java Plug-in may not display errors as you'd expect. If, for instance, you forget to compile the FontMaster applet, you won't see a message indicating that the applet could not be found. Instead, you will see a message from the Java Plug-in: "Loading Applet", and your browser will appear to hang.
If you put such applets on your Web site, you certainly won't endear yourself to your visitors. Thus, when deploying the Java Plug-in, it behoves you to check and double-check, before going live with your deployment.
Help and Options
When users who don't have the Java Plug-in installed visit your newly converted Web pages, they are automatically taken to the Sun Plug-in download page, where they are offered the opportunity to download and install the Java Plug-in.
This is a one-time occurance; the next time they visit your page, the Java Plug-in will automatically load. The HTMLConverter Advanced Options dialog, shown below, allows you to change the location of the page to which the user is referred in such a case.
On a company Intranet, for instance, it may be much faster to download a copy of the Java Plug-in from a server on the LAN, rather than over the Internet. Furthermore, this option allows system administrators to deploy the Java Plug-in from behind a firewall, even when Java applets downloaded from the Internet are filtered out.
Something to Talk About
After reading through this section, what do you think of the Java Plugin and the HTML converter?
- Can you think of one advantage of using the Java Plugin and one disadvantage?
- Under what circumstances would you choose to use the Java 2 platform, along with the Java Plugin, and when would you choose to write your applets in Java 1.1 [or 1.0] instead?
- Assuming you only wrote Java applications--instead of applets. Would you use Java 2 instead of Java 1.1? Why or why not?
This is the last lesson in the course.
|