http://www.developer.com/

Back to article

Where to Start with Java Web Start


July 8, 2009

Ever wondered how to deliver the latest version of a Java application client to a user without the user having to install it? Ever wondered how to launch Java application clients without installing them? The answer is Java Web Start, the deployment solution for Java-based applications. Web Start eliminates the tedious procedures for upgrading existing application versions, and it can ensure that your users are running the latest version.

For the user, invoking Java Web Start is as simple as clicking on the link or typing a URL in a browser. Web Start takes control of the environment and ensures the application is up to date, and the user doesn't have to know about any of the background activities. Based on the Java Network Launching Protocol (JNLP), Web Start lets you specify the version of Java that users must have installed before they can execute the application.

This article provides the basics for you to begin using Java Web Start to deploy your Java application clients.

How Does Web Start Work?

To a provide Web Start-based application, you use a web server. The web server needs to be configured for the MIME type of .jnlp files. That is, the files of type .jnlp must to be set as application/x-java-jnlp-file. This setup invokes Java Web Start whenever a file of type .jnlp is provided to the URL. The web server also must have access to all the files that will be served using Java Web Start. This allows the web server to serve the application without any hassles.

You start Java Web Start by invoking a .jnlp file, which contains a list of supported entities that are required for the application to run. The specified resources will be downloaded to the user's machine first and then executed based on information such as which is the main class, and so on.

For versioning, Java Web Start verifies that the specified files located on the server match those that reside in the user's PC, and if it finds an updated file on the server, it downloads it to the user's PC before starting the application. When the application is launched for the first time, Java Web Start downloads all the related resources from the server and then starts the application.

Using Java Web Start preserves the richness of the application client, as Web Start downloads the complete application client onto the user's machine and launches it using the JRE available on that machine. Generally, this setup provides superior richness to web applications. Of course, there are exceptions.

JNLP File in Detail

The following is a typical .jnlp file that you would use for deployments:
<?xml version="1.0" encoding="utf-8"?> 
<--Demo JNLP file  -->
< jnlp 
 codebase="http://yourUrl.com//webstart" href="myApp.jnlp"> 
  <information> 
    <title>My Web Start Application</title> 
    <vendor>Vendor Name</vendor> 
    <description>Java Web Start Application</description> 
    <icon href="images/myApp.jpg"/> 
  </information> 
  <security> 
      <all-permissions/> 
  </security> 
  <resources> 
    <j2se version="1.4.2"/> 
    <jar href="jars/myApp.jar"/> 
  </resources> 
  <application-desc main-class="MyApp"/> 
</jnlp>

The contents of this file should be sufficient for you to proceed with development. The sub-elements (information, security, and resources) are self-explanatory. They respectively convey information about the application (such as the Vendor, a brief description, etc.), security restrictions, and the resources that are available to the application context for execution.

The other notable elements operate as follows:

  • The tag application-desc indicates that the main method in the class MyApp will be used to launch the application. The tag applet-desc provides support for applets.
  • The codebase attribute in the jnlp tag will use the specified value as the base. The href attribute indicates the .jnlp file to be used for this application.
  • The security element behaves similarly to the restrictions applicable to an Applet. The all-permissions element instructs the runtime to give unlimited access to the application in this context. Using this element also mandates that the JARs in this application must be signed.
  • You can also use the j2se version attribute to specify which version of the JRE will be used to execute this application. You can further enhance this attribute to have an initial heap size that the JRE will use. For example, <j2se version="1.4.2" initial-heap-size="32m"/> specifies that the application has to be launched with version 1.4.2 and have an initial heap size of 32MB. If you wanted to specify that any version of the JRE (>= 1.4.2) can be used, the syntax would be <j2se version="1.4.2+"/>. The plus sign (+) specifies that a JRE version greater than or equal to 1.4.2 can be used.
  • You use the application-desc element to specify the main class for the application. You can also pass arguments using the following attributes:
    <application-desc main-class="MyApp"> 
        <argument>argument-1</argument> 
        <argument>argument-2</argument> 
    </application-desc>

  • Launching an Application Using Java Web Start

    As discussed previously, you need a web page to launch an application with Java Web Start. As an example, take the following code snippet for a HTML file named MyApp.html, which contains a link to launch the application pointing to MyApp.jnlp:
    <html>
      <head> 
          <title>JNLP usage demo!</title>
      </head>
      <body> 
           <a href="MyApp.jnlp">Launch My Application</a>
      </body>
    </html>

    When you invoke the file MyApp.html from a browser with a URL that points to the file (maybe something like http://localhost:8080/MyApp.html), a web page will display the link for "Launch My Application." On clicking this link (as long as the .jnlp MIME type is configured), Java Web Start launches the application MyApp with the parameters provided in the demo JNLP file.

    When Java Web Start Is Not Available on User's System

    During development, you usually have Java Web Start installed on most of your systems, but this may not always be the case during deployment. Many times, end-user systems will not have Java Web Start installed and hence the clients will not behave as you expect. The user will just assume that your application does not work.

    In such cases, you need your application launcher to be at least intelligent enough to tell the user why the application failed to launch. Providing this intelligence requires JavaScript (see below). You must run this script in the MyApp.html as part of the onLoad method. Based on the return type, a corrective action such as displaying a message about the non-availability of Java Web Start will be necessary.

    <SCRIPT LANGUAGE="JavaScript"> 
    var javawsInstalled = 0; 
    isIE = "false"; 
    if (navigator.mimeTypes && navigator.mimeTypes.length) { 
       jnlpTrue = navigator.mimeTypes['application/x-java-jnlp-file']; 
       if (jnlpTrue) { 
          javawsInstalled = 1; 
       } 
    } 
    else { 
       isIE = "true"; 
    } 
    </SCRIPT>

    The above JavaScript verifies whether Java is installed when the browser is Netscape. You can expand the script to cover Internet Explorer as well.

    This article has given you enough information to get started with Java Web Start. When you have mastered the basics, you can accomplish much more with the technology by customizing the .jnlp file and exploring more of its concepts.

    For Further Reading

  • " Java Web Start to the Rescue" (from Sun Developer Network)

    About the Author

    Sridhar M S is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.

  • Sitemap | Contact Us

    Thanks for your registration, follow us on our social networks to keep up-to-date