Advertisment

Web Start your Java Apps

author-image
CIOL Bureau
Updated On
New Update

By: Kunal Jaggi

Advertisment

The Java community has witnessed a transition from applets which dominated the client-side development turf through to server side development technologies like servlets and JSPs, which are an integral part of the Java EE platform. These technologies are primarily constrained by the environment in which they are hosted-a browser. For instance, applets are restricted by the sandbox environment for security reasons. Moreover, markup language-based UI is fairly limited. In this article, we'll show you how a standalone Java application can be made available on a Web server (Apache) and then deployed on a wide-variety of platforms, at the click of a mouse.

Direct Hit!
Applies to: Java SE developers
USP: Deploy browser independent thick clients and run locally on users' desktops
Primary Link: http://java.sun.com/products/javawebstart/
Google keywords: Java Web Start
On PCQEssential CD: System/Labs

What is Java Web Start?

JWS technology is designed exclusively to launch applications written for the Java SE (Java platform Standard Edition). JWS is browser and platform-independent. Thus, it works virtually with all browsers, and supports multiple versions of the Java SE platform. Applications launched with JWS are cached locally. Thus, an already downloaded application is launched on par with a traditionally installed application. As opposed to applets, you don't have to download the application each time it is needed. JWS automatically maintains version information, so download is not required again unless a change is made. JWS requires access through the browser only once when the application is first downloaded. With JWS, applications can also be launched through desktop shortcuts. Further, JWS can be used for offline operation of an application. It leverages the inherent security of the Java platform. Applications are run in a protective sandbox environment with restricted access to system resources like local disk and network resources.

Advertisment

Java Network Launching Protocol

JWS is based on the JNLP (Java Network Launching Protocol) specification. It's an open specification, which recognizes XML files. JNLP provides a Web-centric protocol for deploying and running Java applications. It gives a way for an application to run from a codebase accessible over the Net. It allows one-click application installation and initialization through a browser. The JNLP file specifies the locations (URLs) of the resources on the server for downloading.

Java Web Start Takeaways 
  • Portable and Compatible

  • Web Server Independent

  • Local Caching               

  • Automatic Updates      

  • Browser Independent

  • Offline Access

  • Robust and Scalable

  • Highly Secure

JWS in action

To start distributing applications, some minimal installation and configuration is required both on the server and client ends. First, let's describe server-side configurations. For simplicity's sake, we'll demonstrate a very simple GUI-based Java application that we'll distribute through JWS. Distributing an application is fairly easy, and you have to start this by creating a JNLP file.  A JNLP file describes an application as an XML document as shown in the snippet below

Advertisment





   

       PCQuest - Now on stands

       CyberMedia

      

       PCQuest Home Application

       PCQuest - Now on stands

      

      

    


     

      

      

    


    

The 'codebase' property gives the details about the application. The 'href' attribute points to the location of the JNLP file itself. The information element includes tags for naming the vendor and the application. It also provides a brief description about the application. The icons are used to represent the application when JWS presents the application to the user during launch. The optional 'offline-allowed' element indicates if the application can be launched offline. This element also controls how the JWS checks for an update to an application. If the application is required to be online to run, JWS will always do a check for an updated version before the application is launched. And if an update is found, the new application will be downloaded and launched automatically. JWS will try to check for updates even if 'offline-allowed' is specified.

The 'resources' element is used to specify all the resources, such as Java class, system properties, etc. The version tag illustrates which version of the JRE should be used. Finally, the 'application' element indicates that the JNLP file is launching an application and not an applet. The application element has an optional attribute, main-class, which can be used to specify the name of the application's main class. Please note that if the JAR file specified in the JNLP file contains a manifest file containing the main class, the main-class attribute can be omitted. The next task is to create a HTML file that'll be used to launch the application. The HTML file  requires just a standard link to the JNLP file. The next step towards deployment is to package the application into a JAR file. The following command would pack the example classes.

Advertisment

jar -cvf PCQuestHome.jar magazine.jpg *.class

Place the JNLP and JAR files inside the root directory of your Web application. Please note, in order to use JWS, the Web server must be configured with support the 'application/x-java-jnlp-file' MIME type.  All Apache versions starting from 4.x already have this MIME setting.

publive-image
The user clicks on the link seen on the Web page which automatically starts JWS, before starting the application itself
Advertisment

JWS on the client desktop

At the client-end, install JWS version 1.2 which is shipped as part of the Java SE 1.4. When a user clicks on a link that points to a special launch file (JNLP file), it causes the browser to launch the JWS software, which then automatically downloads, caches and runs the application. A splash screen should appear, followed by a window indicating that the application is being downloaded, as shown in the following figure. Java Web Start downloads the entire application if the application was not previously downloaded. If the application is currently on the client, only new or changed resources are downloaded. After this update check, JWS automatically launches the application locally.

Depending on the options set up for Java Web Start, launching the same networked application will create a desktop shortcut at the discretion of the user.Extracting resources from a JAR file. JWS transfers and stores JAR files to the client machine. An application can't use disk references to access resources such as images and system configuration files. All application resources must be retrieved from the JAR files specified in the 'resources' section of the JNLP. The following code example shows how to retrieve images from a JAR file.

ClassLoader cl = this.getClass().getClassLoader();

Image img=Toolkit.getDefaultToolkit().getImage(cl.getResource("magazine.jpg"));

Conclusion

JWS is a big boost to client-side development that provides a rich user experience decoupled from the browser. Java Web Start (client-side agent) coupled with JNLP (server-side deployment protocol) offers a powerful paradigm to design thick clients which can be distributed over a network, made to run in browser independent fashion, cached locally and those that can be updated automatically with just a single mouse click. JWS empowers you to push upgrades of your applications directly to client desktops. From business standpoint, JWS can significantly minimize the overhead costs associated with deployment and ongoing application support.

Source: PCQuest

tech-news