Java Rich Internet Applications Guide > Migrating Java Applets to Java Web Start Applications
This guide is intended to help developers migrate their existing Java Plug-in applets to Java Web Start applications. While it should be possible to migrate most applets without problem, there are some considerations for developers to be aware of when making the migration. The following guide describes details of the migration process.
The following topics are covered:
Migrating provides the ability to target to a specific Java Runtime Environment (JRE) version or a specific version range. Java Web Start technology supports multiple, simultaneous versions of the Java Standard Edition platform. Specific applications can request specific Java versions without conflicting with the different needs of other applications. Java Web Start technology automatically downloads and installs the correct version of the Java platform as necessary, based on the application's needs and the user's environment.
Another advantage is that users can launch a Java Web Start application independent of a Web browser. A user can be off-line or unable to access the browser. Desktop shortcuts can also launch the application, providing the user with the same experience as that of a native application.
Java Web Start technology has built-in support for applets. It is possible to run your applet directly with Java Web Start technology without any re-compilation of the applet. All you need do is to convert your applet HTML tags to a Java Network Launching Protocol (JNLP) file, using the JNLP applet-desc
element. For example:
<resources> <java version="1.5+"/> <jar href="SwingSet2.jar"/> </resources> <applet-desc main-class="SwingSet2Applet" name="SwingSet" width="625" height="595"> <param name="param1" value="value1"/> <param name="param2" value="value2"/> </applet-desc>
You can specify the targeted JRE versions using the java
element and its version attribute. It supports the +
and *
operators, and you can even list out the exact version. Some examples follow:
<java version="1.5+"/>
<java version="1.5*"/>
<java version="1.5.0_12+ 1.4.2_20"/>
All your applet resources must be packaged inside a JAR (Java archive file), and have the JAR file listed using the jar
element.
The applet-desc
element contains all the applet's information such as applet parameters, width, height, etc. For more information regarding the applet-desc
element, refer to the JNLP specification, section 3.6.2, "Application descriptor for an Applet."
With the applet-desc
tag, Java Web Start technology uses its own version of the applet viewer to start your applet.
The best way to migrate your applet is to re-write it as a standalone Java application, and then deploy it with Java Web Start technology. Re-writing your applet and testing the resulting application ensures that your converted applet works fully as expected. And your application can take advantage of the Java Web Start features.
The re-write should be fairly straight forward. The major work needed is to convert your applet
class to the main
class of the application. The applet init
and start
methods are no longer present, instead, you should initialize the application at the beginning of the application's main
method.
To quickly begin the migration, you can just add the main
method to your original applet
class, and then start calling your applet initialization code where it normally gets called from the applet's init
and start
methods. Once there is a main
method in the applet
class, you can begin launching it by means of Java Web Start technology, and then slowly remove the dependency on the Applet
class and convert it completely to your application's main
class.
For more information, refer to JNLP File Syntax.
Following is a list of considerations that may be important when migrating.
A Java Web Start application does not run within the web browser. So if your applet has any dependency on the browser (for example, Java to JavaScript / JavaScript to Java communications by means of the browser), the communication code will no longer work. The APIs that are affected include:
JSObject
API (netscape.javascript.JSObject.*
) for Java to JavaScript communication does not work for Java Web Start applications.
Common Document Object Model (DOM) API (com.sun.java.browser.dom.*
and org.w3c.dom.*
) available for Java Plug-in applets are not available to Java Web Start applications.
Similar to Java Plug-in technology, Java Web Start technology will cache your application JARs for faster start-up performance. However, resources downloaded by your own application code will not be cached by Java Web Start technology.
Java Web Start technology provides permanent cookie support on Windows using the cookie store in Internet Explorer, and the cookie-handling behavior is determined by the cookie control in IE. On Linux/Solaris, Java Web Start technology provides permanent cookie support using its own cookie store implementation. For more information, see Cookies in the Java Tutorials.
If you deploy an applet with the JNLP applet-desc
element, your applet will be created using the AppletContainer
provided by Java Web Start technology. When your applet calls Applet.getAppletContext()
, it returns the AppletContainerContext
provided by Java Web Start technology. There are some minor differences in implementation between the Java Plug-in AppletContext
and the Java Web Start AppletContext
. The differences are:
The following Applet Persistence API methods are not implemented by Java Web Start technology:
AppletContext.getStream(String key) AppletContext.getStreamKeys() AppletContext.setStream(String key, InputStream s)
For Java Web Start applications, you can use the JNLP Persistence Service API for storing data locally on the client's system. For more information, see the PersistenceService
interface.
For AppletContext.showDocument(URL url, String target)
, the target argument will be ignored by Java Web Start technology.
For AppletContext.showStatus(String status)
, when launched with Java Web Start technology, this will set the JLabel
text that is below the applet, hosted by the Java Web Start AppletContainer
.
Similar to AppletContext.showDocument
, Java Web Start applications are capabile of showing an HTML page using the system's default web browser by using the BasicService.showDocument
API.
For a Java Plug-in applet:
AppletContext.showDocument(URL url) AppletContext.showDocument(URL url, String target)
For a Java Web Start application:
BasicService.showDocument(URL url)
For more information, see the BasicService
interface.
In an applet, if you obtain a resource by means of these calls:
Applet.getImage(URL url) Applet.getImage(URL url, String name) Applet.getAudioClip(URL url) Applet.getAudioClip(URL url, String name) AppletContext.getAudioClip(URL url) AppletContext.getImage(URL url)
Then in Java Web Start technology, the best practice is to include the resources in your application JAR files, and access the resources using the JNLPClassLoader
:
ClassLoader cl = this.getClass().getClassLoader(); URL url = cl.getResource(url); Image im = Toolkit.getDefaultToolkit().createImage(url);
For more information, see Retrieving Resources from JAR Files.
The pack200 JAR packing tool is supported by both the Java Web Start and the Java Plug-in technologies. If you are already deploying your applet JARs with pack200, no change should be needed when migrating to Java Web Start technology. For more information, see Deploying JAR Files Compressed with Pack200.
By using the OBJECT
tag in Java Plug-in technology, you can detect whether Java is avaliable on the client's machine with the Plug-in CLSID
and then auto-download Java if necessary. The same support is available with Java Web Start technology by using the Java Web Start CLSID
. For more information, see Ensuring the Presence of the JRE Software in the Java Tutorials.
If you want to deploy extensions for your Java Web Start application, use the JNLP extension protocol mechanism.
One advantage of the JNLP extensions mechanism over Java Plug-in technology is that the installed extensions will be available to all Java Web Start applications running on the system, no matter what version of JRE the application is running with. While for Java Plug-in technology, only applets running in the same JRE version can make use of the installed extensions.
For signed JAR files, similar to Java Plug-in technology, you can sign your application JAR files and request your application to be run with all-permissions by means of the JNLP file. In Java Plug-in technology, your applet JARs can be signed by different certificates. In Java Web Start technology, the same certificate must be used to sign all JAR files (jar
and nativelib
elements) that are part of a single JNLP file. This simplifies user management since only one certificate must be presented to the user during a launch per JNLP file. If you need to use JARs signed with different certificates, you can put them in a component extension JNLP file, and reference it from the main JNLP file.