Distributing Java programs

Java Applets
Java Web Start

Based on notes Distributing Java programs

Java applets

An example

  • Applets are embedded in another program, typically a web browser, and distributed through a web server.

  • The range of operations applets can perform is somewhat restricted. File operations and printing are not allowed until the applet is digitally signed.

Writing applets

  • Applets can created by writing a subclass of JApplet, which is itself a subclass of Panel. The application running the applet, usually a web browser, will provide a Frame in which the applet will reside.

  • Components are generally added to the JApplet rather than painting directly onto it.

  • Applications "stand alone". The execution of the code clearly begins with public static void main(String[] args).

  • It may not be immediately clear where the execution of an applet begins. This is because the applet is being run by another program, such as a web browser.

Converting applications to applets

  • Typically speaking, we do not write a constructor for a JApplet nor we do not instantiate it explicitly. The web browser will do this.

  • The initialization is performed by overriding the method public void init().

  • Converting an application to an applet is usually as simple as moving the content in the application's main method to the applet's init method.

An example

Here is an example of how an application, our CubicGraph from Chapter 3, can be converted to an applet.


Another example


Preparing the HTML file


Testing the applet

You can test the applet using a program called appletviewer:

    appletviewer lineMover.html


In order to insure that the applet runs in as diverse an environment as possible, use the Java program HtmlConverter.

    HtmlConverter lineMover.html

Java Web Start

Java Web Start gives a way to distribute Java applications conveniently.

First, your web server needs to be reconfigured to recognize the JNLP mime types. On the apache server, this is as simple as adding the line

    application/x-java-jnlp-file JNLP
to the file /etc/mime.types.

Preparing the jar file

The application needs to be packaged in a "jar" file that is digitally signed.

  • Put all the application's class files in a jar file:

    	jar cvf jarfilename.jar classfilenames

  • Create a public/private key pair using keytool:

    	keytool -genkey -alias david

  • Create a self-signed certificate using keytool:

    	keytool -selfcert -alias david

  • Sign the .jar file using another Java program called jarsigner:

    	jarsigner myjar.jar david

Create a JNLP file

Here is an example


Download the application.

Using Web Start as a client

To use applications delivered by Java Web Start, we need to install Web Start locally.

Web Start is installed automatically when the Java 1.4.1 JRE or SDK is installed on a Windows machine.

Under linux, it is as simple as copying a file named something like


into a directory in your home directory, unzipping the file and running install.sh.


Web Start allows the user to run the application locally. In addition, it will check for updates and download them when they are available.