Distributing Java programs

Java Applets
Java Web Start

Based on notes Distributing Java programs
ADDANCHOR applets FIRSTANCHOR 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.
ANCHOR 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.
ANCHOR 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.
ANCHOR An example

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

ANCHOR Another example

ANCHOR Preparing the HTML file

ANCHOR Testing the applet

You can test the applet using a program called appletviewer:

    appletviewer lineMover.html
ANCHOR HtmlConverter

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

    HtmlConverter lineMover.html
ADDANCHOR webstart ANCHOR 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. ANCHOR Preparing the jar file

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

ANCHOR Create a JNLP file

Here is an example


Download the application. ANCHOR 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. LASTANCHOR Advantages

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