| # JavaTM Web App Deployment Advice for JavaSE 6u10 | Documentation Contents

Java Web App Development and Deployment > Java Web App Deployment Advice

Contents

- Unified Deployment Mechanism - Java Network Launch Protocol
- Deployment Toolkit
- Deploying Java Plug-In Applets
  - Using Applet Tag Attributes
  - Using JNLP
  - Determining final deployment information
- Deploying Java Web Start Applications
- Pack200 and Version Download
- Other Deployment Considerations
  - Order of Installation of JRE's
  - ClassLoader and Accessing Resources
  - Lazy Downloading

## Unified Deployment Mechanism - Java Network Launch Protocol

Java Network Launch Protocol (JNLP) provides a unified mechanism for deploying applets and Java Web Start applications. Solutions launched using JNLP have the powerful capabilities at their disposal:

- Access to JNLP extensions: Powerful JNLP extensions such as the Java binding to the OpenGL 3D API, the scene graph for the JavaFX run-time, and the NASA World Wind Java planetary and extra-planetary visualization system can now be used from applets and Java Web Start applications.
- Access to the JNLP APIs: Applets and Java Web Start applications have access to the JNLP APIs to gain user-controlled access to persistent storage (the PersistenceService), lazily-downloaded parts (the DownloadService), the local file system (the FileOpenService and FileSaveService), and other useful functionality. Now all of this functionality is transparently available to applet developers.
- Auto-download, version selection and more: Building on the new Java Plug-In's architecture, applets launched via JNLP may utilize all of the features previously available to Java Web Start applications, such as JRE version selection, auto-download of a particular version of the JRE, passing command-line arguments and system properties to the JVM, and much more.

## Deployment Toolkit

To avoid browser compatibility issues, the Deployment Toolkit (deployJava.js) provides JavaScript functions that automatically generate HTML required to deploy applets and Java Web Start applications. Developers should invoke these functions to deploy their solutions in a consistent fashion across various browsers.

The script exposes a single object, named deployJava, which contains the following public functions:

- `getJREs()` - Returns a list of currently-installed JRE versions.
- `installLatestJRE()` - Triggers the installation of the latest JRE
- `installJRE(requestVersion)` - Triggers the installation of the specified `requestVersion`, the latest version matching the specified `requestVersion`, or the latest JRE.
- `versionCheck(version)` - Returns true if there is a matching JRE version currently installed (among those detected by `getJREs()`).
- `writeAppletTag(attributes, parameters)` - Outputs an applet tag with the specified attributes and parameters. The parameters argument is optional.
- `runApplet(attributes, parameters, minimumVersion)` - Ensures that an appropriate JRE is installed and then runs an applet.
- `isWebStartInstalled(minimumVersion)` - Returns true if an installation of Java Web Start of the specified `minimumVersion` is found.
- `createWebStartLaunchButton(jnlp, minimumVersion)` - Outputs a launch button for the specified JNLP URL. When clicked, the button will ensure that an appropriate JRE is installed and then launch the JNLP application.
- `setInstallerType(type)` - Sets the preferred install type : null, online, kernel.
- `setAdditionalPackages(packageList)` - Sets additional package list to be used by kernel installer.

See deployJava.js for detailed description of the version, attributes and parameters of the above mentioned public functions.

## Deploying Java Plug-In Applets

With recent changes to the Deployment Security Policy, Java Plug-In applets will normally run with only the latest deployed Java version on a client machine. With Java SE 6 update 10, you can specify that a Java kernel be automatically downloaded, if the required version (1.6.0_10) or higher is absent. The Java kernel is a core set of Java classes absolutely required by the Java Runtime Environment. Any additional packages may also be specified for download as required by the applet.

### Using Applet Tag Attributes

Applets can be deployed via the applet tag. Parameters to configure deployment may be specified as attributes and parameters to the applet tag.

Use the `runApplet()` function in `deployJava` to ensure that a minimum Java Runtime Environment is available on a client machine before launching the applet.

    <script src="http://java.com/js/deployJava.js"></script>
    <script>
        var attributes = {codebase:'http://java.sun.com/products/plugin/1.5.0/demos/jfc/Java2D',
                          code:'java2d.Java2DemoApplet.class',
                          archive:'Java2Demo.jar',
                          width:710, height:540} ;
        var parameters = {fontSize:16} ;
        var version = '1.6' ;
        deployJava.runApplet(attributes, parameters, version);
    </script>

The above code will launch the Java 2D applet on JRE version 1.6.0 or higher with one parameter (fontSize).

To trigger the installation of Java kernel and additional packages, add the following before the deployJava.runApplet() function:

       deployJava.setInstallerType('kernel');
       // include any required packages as shown below
       deployJava.setAdditionalPackages('javax.swing, javax.xml');

Click the following link to run the Java 2D Demo Applet.

### Using JNLP

To deploy applets using JNLP, specify the jnlp_href parameter in the applet tag as follows

    <applet width="300" height="300" code="com.mycompany.MyApplet">
      	  <param name="jnlp_href" value="my_applet.jnlp">
    </applet>

In this example, my_applet.jnlp has the following deployment information

     <?xml version="1.0" encoding="UTF-8"?>
     <jnlp href="my_applet.jnlp">
         <information>
           <title>My Applet</title>
           <vendor>My Company, Inc.</vendor>
           <offline-allowed />
         </information>
         <resources>
           <j2se version="1.4+"
                 href="http://java.sun.com/products/autodl/j2se" />
           <jar href="my_applet.jar" main="true" />
           <extension name="SomeExtension"
                      href="http://some.server.com/some_extension.jnlp" />
         </resources>
         <applet-desc
             name="My Applet"
             main-class="com.mycompany.MyApplet"
             width="300"
             height="300">
         </applet-desc>
     </jnlp>

#### Determining final deployment information

The applet tag and JNLP file provide overlapping mechanisms to specify the same information. For example, width, height, code etc. may be specified both as attributes of the applet tag and in the JNLP file. The following rules are applied to various attributes in order to determine the final deployment information:

- `width` and `height`: These attributes are always taken from the `<applet>` tag and not from the JNLP file. The assumption is that the browser knows best how big the applet should be on the web page, and only the browser can support page-relative width and height (for example, `width="50%"`).
- `codebase`: It is recommended that you either:
  - leave the jnlp tag's codebase attribute empty for both the main and extension JNLP file, allowing the referencing JNLP file or `<applet>` tag to implicitly specify the codebase, or
  - specify an absolute URL for the codebase of any main or extension JNLP file

  Refer to CodeBase Determination for further information.

- `code`: The `code` attribute in the `<applet>` tag will be ignored.
- Any applet parameters specified using the `<param>` tag are merged with those specified in the JNLP file. If the same parameter is specified via both the `<applet>` tag and the JNLP file, the `<applet>` tag's version overrides the JNLP file's version, except for the `java_arguments` and `java_version` parameters.
- The new `java_arguments` and `java_version` parameters are unnecessary in JNLP applets. The mechanisms in the JNLP file for requesting a JRE version, or passing command-line arguments to the JVM, are used instead. Command-line arguments and JRE version requests from the JNLP file override those specified in the HTML for the applet.
- It may be advantageous to specify certain parameters desired early on in the applet's startup process, such as `image`, `boxbgcolor`, etc. in the HTML instead of in the JNLP file, because these are available immediately upon loading the web page rather than requiring the JNLP file to be downloaded separately first.

## Deploying Java Web Start Applications

A Java Web Start Application can be deployed simply by creating a jnlp file that describes only the title, vendor, java version, jar file(s), and main class of the application. Here is an example of a minimal jnlp file:

>     <jnlp>
>         <information>
>             <title>Notepad</title>
>             <vendor>Sun Microsystems</vendor>
>         </information>
>         <resources>
>             <java version="1.4+"/>
>             <jar href="http://java.sun.com/products/javawebstart/apps/notepad.jar"/>
>         </resources>
>         <application-desc main-class="Notepad"/>
>     </jnlp>

The application can then be deployed simply by providing a link to the jnlp file on your web page:

>     <a href="http://java.sun.com/products/javawebstart/apps/notepad.jnlp">Launch Notepad</a>

Many other elements can be added to the jnlp file to control the user experience, security, and update process of the application, or to take advantage of several other features of the Java Network Launching Protocol (JNLP).

Java Web Start can use its Auto-Download mechanism to download the version of the JRE that it requires to run an application, but if the application wants to use advanced features of JNLP and Java Web Start that were added to a particular version, you may want to ensure that at least that version is installed before launching Java Web Start.

Suppose an application wants to use the SingleInstance Service (introduced in 1.5.0). Instead of just specifying <jnlp spec="1.5.0" .../> and letting the application fail on systems where only 1.4.2 or earlier is installed, you can use the deployJava javascript to ensure that at least version 1.5.0 is installed before launching Java Web Start.

>     <script src="http://java.com/js/deployJava.js"></script>
>     <script>
>         var url = "http://java.sun.com/products/javawebstart/apps/notepad.jnlp";
>         deployJava.createWebStartLaunchButton(url, '1.6.0');
>     </script>

Click the Launch button to run the notepad demo using Java™ Web Start.

For an application not having specific version requirements you can just use the function without supplying minimumVersion.

    <script>
        var url = "http://java.sun.com/products/javawebstart/apps/notepad.jnlp";
        deployJava.createWebStartLaunchButton(url);
    </script>

## Other Deployment Considerations

### Order of Installation of JRE's

If multiple JRE's are required to run various Java Plug-in applets on the same machine, it is recommended to install the JRE's in the order of their versions. The oldest version should be installed first and the newest version installed last. This will avoid the problem of the dynamic clsid {8AD9C840-044E-11D1-B3E9-00805F499D93} being used in an object tag that is not using the latest version of the JRE on the machine.

Starting from JRE 5.0u6 with SSV support, the above is not an issue because the latest version of JRE on the machine will be used. In addition, we have added a new dynamic version clsid {CAFEEFAC-FFFF-FFFF-FFFF-ABCDEFFEDCBA}. If the new dynamic clsid is used in the object tag, the latest version of the JRE will be used independently of the installation order of the JRE's.

Installation order should have no effect on Java Web Start. In any case the highest version of the JRE on the system will contain the version of Java Web Start that is run.

### ClassLoader and Accessing Resources

Resources accessed in a Java Web Start application or Java Plug-in applet may be cached on the client machine in the Deployment Cache. It is unwise to assume the format or content of this cache, as it may change between versions.

When porting stand alone programs to Java Web Start or Java Plug-in, problems can occur when code has inherent assumptions that it is loaded by the SystemClassLoader. In Java Plug-in resources are loaded by the PluginClassLoader (which extends sun.applet.AppletClassLoader, which in turn extends java.net.URLClassLoader). In Java Web Start resources are loaded by the JNLPClassLoader (which as of JDK 6 extends java.net.URLClassLoader).

Access the ClassLoader being used with:

>     ClassLoader cl = Thread.getCurrent().getContextClassLoader();

ClassLoader.getResource() returns a URL, but any code that assumes the URL is a JarURL to a FileURL, and then tries to decompose that FileURL to find the underlying file path will fail. The correct way to access resources is to use getResourceAsStream() which will return the correct content whatever type of ClassLoader is used to access the resource. If the resource is already cached, the contents of the resource will be returned from the cache directly, so there won't be extra network connections to the resource itself.

We do not recommend modifying the contents of the Java deployment cache directly. The cache is a private implementation of Java Web Start / Java Plug-in, and is subject to change anytime.

Many applications and libraries try to deploy properties files and other "resources" by including them in the same directory as the jar file that uses them, and then expect to be able to decompose the the URL returned from getResource() to construct the path to these files. Developers argue that this is needed so the application can later modify these property files or other "resources" for use in subsequent launchings of the app. When porting to web deployed applications, they then find they need to repackage these into the jar files of the app, and consider them only the "default" content, and use one of several other mechanisms to persist the modified versions on the client machine (by writing files or by using either the Preference API or the JNLP PersistenceService.)

### Lazy Downloading

When applications are large, it can be useful to only download the part of the application that is required to start up, and then download the rest on demand. This process is referred to as lazy downloading.

Java Web Start has support for lazy downloading, but few developers use it. It can be a way to significantly improve the download and startup time in some applications. To effectively use lazy downloading, Java Web Start must be aware which jar to download to resolve a request for a specific resource. Previous versions of Java Web Start required a complex specification of parts and packages to provide this information. Beginning with version 6.0, the same thing can be accomplished using Jar Indexing.

Jar Indexing is a simple and effective way to download only the required jars, and avoid downloading everything when a nonexistent resource is requested. See Jar Indexing.

Java Plug-in has built-in support for lazy downloading (that is, downloading is lazy by default), and also supports Jar Indexing. Developers should also try to NOT use individual classes but package them as JARs instead.

##

Copyright ©2008 Sun Microsystems, Inc. All Rights Reserved. Feedback | Java Technology