Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

JVM Languages

Jan02: Java Q&A

Jan02: Java Q&A

How Do I Use the Java Plug-In Tool?

Mike is an independent consultant and lecturer. He is also director of Southgate Software Ltd. He can be reached at [email protected].

In May 1995, Netscape Communications added to its browser a Java interpreter and the ability to parse the newly added <applet> tag, bringing to the Web a standard cross-platform way to do client-side computation and presentation. Microsoft, wary of anything cross platform but still wanting to match Netscape's browser feature for feature, licensed Java technology from Sun Microsystems in March 1996 for its Internet Explorer (IE) browser.

As the web-browser market evolved, differences between Java virtual machines meant that some applets would behave differently on different browser/operating-system combinations — hardly a "write once, run everywhere" situation. In addition, Microsoft's Java implementation was frozen at Version 1.1 due to a legal battle with Sun, while Java itself moved from Version 1.2 to 1.3 to 1.4, and so on.

To let programmers and end users take full advantage of state-of-the-art Java while keeping behavior consistent, Sun developed a freely available Java plug-in tool. The Java plug-in is available for many OS/browser combinations, most notably Netscape and IE on Windows and Netscape on Linux (http://java.sun.com/products/plugin/). In this article, I'll describe how to use the Java plug-in to deploy Java 1.3-enabled applets.

What Is a Plug-In?

Every group of bytes or object downloaded from web servers by web browsers have properties that browsers can use. The most important property a downloaded object has is its "content type," which is a string indicating that object's Multimedia Internet Message Extensions (MIME) type. For example, a standard web page has a content type of text/html, whereas a JPEG image has a content type of image/jpeg.

The browser knows how to handle many of these MIME types, but if it encounters a MIME type that it cannot process internally, it looks for an external piece of code registered to handle that type. That piece of code is a plug-in.

Object or Embed?

Netscape-style plug-ins are activated by specifying an <embed> tag within a web page. Microsoft-style plug-ins are activated by specifying an <object> tag within a web page. The general strategy then is to determine whether to use an <embed> or <object> tag by using JavaScript to "sniff out" the flavor of browser it's running on. Sun provides a tool that automates this process by automatically transforming a web page with an <applet> tag into a page with the appropriate JavaScript "sniffing." The drawback to using the HTML-converter tool is that the generated HTML is quite difficult to edit, especially if you have an applet with many parameters.


After examining the output from successive generations of Sun's HTML converter, I've developed a boilerplate for new applets. Listing One is the HTML/JavaScript used to launch the SwingSet2 demonstration applet that comes with Sun's JDK 1.3. If you have access to a web server and the SwingSet2.jar file, you can try it on your own web page, or you can try it on my web server at http://southgatesoftware.com/plugin/SwingSet2.html.

Making It Painless

If users browsing the Web with a Netscape browser hit a page with an <embed> tag that refers to a particular plug-in, that plug-in is used automatically if it is already installed; otherwise, users are directed to a page where they can download and install the plug-in if they choose. This page is specified by the pluginspage parameter of the <embed> tag.

Things are both different and more convenient with Microsoft's IE. If users hit a page with an <object> tag that refers to a not-yet-installed plug-in, IE automatically downloads, extracts, and runs a Windows executable contained in a CAB file referenced by the codebase parameter of the <object> tag. The CAB-contained executable can contain a setup program for the entire plug-in, or contain a smaller executable that downloads the rest of the plug-in from a predetermined location while presenting users with a reassuring download status indicator of the autoinstaller in action. This is exactly what Sun has done with its plug-in autoinstaller.

Not for the Faint of Heart

The Java plug-in autoinstaller mechanism downloads the Java plug-in from http://java.sun.com/ and nowhere else. Most of the time this is sufficient, but if you are deploying your applet on an intranet (especially if your intranet has no access to the outside world), you need to change the way the Java plug-in autoinstaller works. In the list of "Available Locations" for downloading, there are two entries: one for java.sun.com and one for southgatesoftware.com. To do this yourself means venturing into the murky waters of CAB files and code signing.

CAB Files and Code Signing

IE's autoinstall mechanism won't work unless the CAB file referenced by the "codebase" parameter of the <object> tag has been digitally signed. To sign a CAB file, you need to download some code signing utilities from Microsoft, as well as obtain a private key and digital certificate file from a certificate authority such as Verisign or Thawte. Once these files are in place, you sign your CAB file as in Listing Two. The signcode utility is available as a free download from Microsoft (http://msdn.microsoft.com/downloads/default.asp?url=/code/sample.asp?url=/msdn-files/027/000/218/msdncompositedoc.xml). Also, you can search Microsoft's web site for "Authenticode for Internet Explorer."

To create or modify CAB files, you also need the cabarc utility, which is also available as a free download from Microsoft by the name of "cabinet sdk." (Visual Studio may also include the cabarc utility.)


Listing One

    var _info = navigator.userAgent; var _ns = false;
    var _ie = (_info.indexOf("MSIE") > 0 && _info.indexOf("Win") > 
                             0 && _info.indexOf("Windows 3.1") < 0);
<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) || (_info.indexOf("Linux") > 0)));
    var _ns6 = ((_ns == true) && (_info.indexOf("Mozilla/5") >= 0));
  var i;
  var _name="SwingSet2";
  var _codebase=".";
  var NN=(document.layers ? true : false);
  var _embedtype="application/x-java-applet;version=1.3";
  var _classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93";
  var _iecodebase="http://southgatesoftware.com/javaplugin/1.3/
  var _nspluginspage="http://home.netscape.com/plugins/jvm.html";
  var _width="100%";
  var _height="100%";
  var _class="SwingSet2Applet";
  var _align="center";
  var _vspace=0;
  var _hspace=0;
  var _jarfiles="SwingSet2.jar";
  var scrap,scrap2;
  var appletProps=new Array();
  var appletValues=new Array();

if (_ie == true)
    document.writeln('<OBJECT classid="'+_classid+'" ');
    document.writeln('WIDTH='+_width+' HEIGHT='+_height);
    document.writeln('NAME='+_name+' ALIGN='+_align);
    document.writeln('VSPACE='+_vspace+' HSPACE='+_hspace);
    document.writeln('<PARAM NAME=CODE VALUE="'+_class+'" >');
    document.writeln('<PARAM NAME=CODEBASE VALUE="'+_codebase+'" >');
    document.writeln('<PARAM NAME="cache_option" VALUE="Plugin">');
    document.writeln('<PARAM NAME="cache_archive" VALUE="'+_jarfiles+'">');
    document.writeln('<PARAM NAME="NAME" VALUE="'+_name+'" >');
    document.writeln('<PARAM NAME="type" 
    document.writeln('<PARAM NAME="scriptable" VALUE="true">');
    document.writeln('<PARAM NAME="mayscript" VALUE="true">');
    for (i=0;i<appletProps.length;i++)
      scrap='<PARAM NAME="'+appletProps[i]+'" VALUE="'+appletValues[i]+'" >';
else if (_ns == true)
    if (_ns6 == true)
        document.writeln('<APPLET ');
        document.writeln('CODE = "'+_class+'" ');
        document.writeln('CODEBASE = "'+_codebase+'" ');
        document.writeln('ARCHIVE = "'+_jarfiles+'" ');
        document.writeln('WIDTH='+_width+' ');
        document.writeln('HEIGHT='+_height+' ');
        document.writeln('ALIGN='+_align+' ');
        document.writeln('VSPACE = '+_vspace+' HSPACE = '+_hspace);
        for (i=0;i<appletProps.length;i++)
            scrap='<PARAM NAME="'+appletProps[i]+'" 
                                           VALUE="'+appletValues[i]+'" >';
        document.writeln('<EMBED type="'+_embedtype+'"  ');
        document.writeln('CODE = "'+_class+'" ');
        document.writeln('CODEBASE = "'+_codebase+'" ');
        document.writeln('NAME = "'+_name+'" ');
        document.writeln('WIDTH='+_width+' HEIGHT='+_height+' 
                                                ALIGN='+_align+' ');
        document.writeln('VSPACE = '+_vspace+' HSPACE = '+_hspace);
        document.writeln('cache_option="Plugin" ');
        document.writeln('cache_archive="'+_jarfiles+'" ');
        document.writeln('scriptable=true ');
        document.writeln('mayscript=true ');
        for (i=0;i<appletProps.length;i++)
            scrap=appletProps[i]+'="'+appletValues[i]+'" ';
You should see a test applet above

Back to Article

Listing Two

signcode -v c:\mykey.pvk -spc c:\mycert.spc file.cab -t 

Back to Article

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.