Demo Applet & Application

Click to start applet

Source Code

Download tar.gz, zip

Using Java applets and applications portably

Part 2: Javascript techniques to control Java applet deployment

Oracle provides a convenience script called deployJava.js to handle differences in the way a Java applet is invoked across browsers. However, it automatically redirects the reader to Oracle's Java download site if the Java runtime is not installed on the reader's machine. As described in the previous article, this results in a poor experience for first time readers, as they are not given the opportunity to skim the page to decide if it's worth installing Java to see the applet run. If the running applet is not integral to the value of the page, it may often be unnecessary to actually run the applet on page load. This article describes how to use a modified Javascript launch framework that ovecomes this problem.

The demo applet on the right shows the effect of the modified Javascript framework. Note that the applet in the box is not launched until the 'Launch Applet' button is pressed.

Using the jnlpapplet framework

We first describe how to control applet launch using the modified Javascript framework provided here.

  1. Download the jnlpapplet package as a tar.gz or zip archive and extract the contents of the file. On Unix like systems (e.g. Linux or Mac OS X) you can extract the contents of the tar archive as follows:
    tar -zxvf jnlpapplet-1.0.tar.gz
    Place the following files from the package in a publicly acesssible location on your web server:
    1. deployJavaExt.js: This file contains the javascript code to portably launch applets across browsers. It may be prefrable to minify the script before use in a production environment.
    2. jnlpapplet.css: This file contains CSS elements that can be used to render the applet window and launch buttons. You can modify the css to suit your website's design.
    3. jnlpapplet.jar: This Java archive file contains the java framework classes needed to launch applets and applications seamlessly. The code here is described in the previous article
    4. jnlpapplet.jnlp: The JNLP (Java Network Launch Protocol) file that specifies how an applet is launched. You can modify this to suit the needs of your website.
    5. jnlpapplication.jnlp: The JNLP (Java Network Launch Protocol) file that specifies how a Java Web Start application is launched. You need to modify this to launch the application from your server and to suit the needs of your website.

  2. Add references to the javascript code and CSS stylesheet to the HTML header of your web pages. You can do this by adding the following lines to the HTML header section of your web pages.
      href="jnlpapplet.css" />
  3. Specify the applet parameters in the page. The example below shows how this can be done. This bit of Javascript needs to be added to the HTML header of the web page below the script references described above.
        <script type="text/javascript">
          var jnlpapplication_url = 
            + 'jnlpapplication.jnlp';
          var attributes = {
            code: 'm42/jnlpapplet/JNLPAppletApplication.class',
            archive: 'jnlpapplet.jar',
            width: 300,
            height: 12, 
          var parameters = {
            fontSize: 12,
            background: '#f5f5dc',
            jnlp_href: 'jnlpapplet.jnlp'
          var version = '1.6';
          var appletCreated = new Array();
    Each variable initiallized in the code above is explained below.
    1. jnlpapplication_url: This variable specifies the URL of the jnlpapplication.jnlp file on your web server. The same URL must be embedded in codebase section of the jnlpapplication.jnlp file. (See below.)
    2. attributes: This is an array of attributes passed to the Java browser plugin that starts the applet. The code attribute and the archive attribute are required. They specify the class to be run and the jar file in which it exists, respectively. The height and width attribute specify how tall and and wide the applet window should be.
    3. paremeters: This is an array of values that the Java plugin passes to the applet. The demo application takes 3 parameters. The jnlp_href parameter is required and is used by the javax.swing.JApplet framework class to retrieve further information from the JNLP file. The fontSize and backround parameters are used by this demo applet to set the font size of the text in the applet window and the background respectively. Your code may use other parameters that may be interpreted differently.
    4. version: This is set to the minimum acceptable version of the Java runtime. If the version of the runtime installed on the readers machine is lower than this, the reader will be redirected to the Java download page and prompted to install or upgrade to the latest version.
    5. appletCreated: This variable indicates which applets on the page have been created. It should always be initialized to an empty array.
  4. Add HTML and CSS elements for the applet and the launch button. The HTML snippet below will result in an unlaunched applet window, with some initial text.
      style="width: 300px;">
      Click to start applet
    <div class="button-css">
      <a href="javascript: createApplet('applet0')">
        Launch Applet
    The first div creates the applet window, while the second div creates the applet launch button. The id attribute (applet0)identifies the applet on the page. If you have more than one applet on the page use a different id for each applet. The class applet-window is defined in jnlpapplet.css and renders an empty box around the area where the applet will be placed. The width and height styles should be specified to match that specified in the javacript attributes passed to the java plugin to ensure that the window dimensions don't change when the applet is started by the user. When the user clicks the launch button, the applet is launched with a call to createApplet that will, if necessary, install the Java runtime on the user's machine.
    To launch a Java Web Start application instead, use the following HTML snippet:
    <script type="text/javascript"<
      var js = deployJava.webStartLaunchButtonJS(
        jnlpapplication_url, version);
        '<a href="' + js
        + '" onMouseOver="window.status=\'\'; '
        + 'return true;">Launch Application</a>');

How the framework works

The key difference between the delayed launch framework provided here and Oracle's original Javascript is that, this version provides methods that work after the page has been rendered. The createApplet() method takes the id of the HTML div element to be replaced. It generates the HTML applet tag appropriate for the browser and replaces the specified div element with the applet tag. The key bit of code in the script accomplishes this is shown below.

  var appletTag 
    = startApplet + '\n' + params + '\n' + endApplet;
  parentTag.innerHTML = appletTag;

You may examine the details of the code by downloading the complete source code and documentation for the framework a tar.gz or zip archive.

The next article in this series will examine how the CSS launch buttons are created.