Creating a Java WebStart (JNLP) application

Java WebStart, also called Java Network Launching Protocol (JNLP), allows you to launch Java applications directly from the internet using a webbrowser. In this article we will create a simple application and all configuration files necessary to launch it through Java WebStart.

We start with a simple Java program with a GUI:

package nl.jansipke.samplegui;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class SampleGUI extends JFrame {

    private static final long serialVersionUID = 522159447010444143L;

    public SampleGUI() {
        setTitle("Sample GUI");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                SampleGUI sampleGUI = new SampleGUI();
                sampleGUI.setVisible(true);
            }
        });
    }
}

Java WebStart applications run in a sandbox with very limited default capabilities. The application can ask for extra permissions in the JNLP file, as can be seen in the following file.

<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="http://www.jansipke.nl" href="SampleGUI.jnlp">

    <information>
        <title>SampleGUI</title>
        <vendor>Some Vendor Name</vendor>
        <homepage href="http://www.jansipke.nl"/>
        <description>SampleGUI description</description>
    </information>

    <security>
        <all-permissions/>
    </security>

    <resources>
        <j2se version="1.6+"/>
        <jar href="SampleGUI.jar"/>
    </resources>

    <application-desc main-class="nl.jansipke.samplegui.SampleGUI"/>

</jnlp>

The JAR file needs to be signed for these permissions to take effect. We need to create a keystore file for that first. If needed you may change the alias and the keystore file name.

keytool -genkey -alias alias -keystore keystore.bin

Answer the questions the keytool command asks and copy the file into a directory where the following ANT build script can find it:

<?xml version="1.0" encoding="UTF-8"?>
<project name="samplegui" basedir=".">

    <property name="dir.build" value="bin" />
    <property name="dir.dist" value="dist" />
    <property name="dir.src" value="src" />
    <property name="file.jar" value="SampleGUI.jar" />

    <path id="compile.classpath">
        <fileset dir=".">
            <include name="lib/*.jar" />
        </fileset>
    </path>

    <target name="clean" description="Clean project">
        <delete dir="${dir.build}" />
    </target>

    <target name="prepare" description="Prepare project">
        <mkdir dir="${dir.build}" />
    </target>

    <target name="compile" description="Compile project" depends="prepare">
        <javac destdir="${dir.build}" classpathref="compile.classpath" debug="true" includeantruntime="false">
            <src path="${dir.src}" />
        </javac>
    </target>

    <target name="jar" description="Build jar file" depends="compile">
        <mkdir dir="${dir.dist}" />
        <jar destfile="${dir.dist}/${file.jar}" basedir="${dir.build}">
            <manifest>
                <attribute name="Main-Class" value="nl.jansipke.samplegui.SampleGUI"/>
            </manifest>
        </jar>
    </target>

    <target name="signjar" description="Sign jar file" depends="jar">
        <signjar jar="${dir.dist}/${file.jar}" alias="alias" storepass="secret" keystore="keystore.bin"/>
    </target>

</project>

Now run the ANT script (target signjar) and copy the resulting JAR file and the JNLP file to a directory on your webserver. Fire up a webbrowser and point it to the JNLP file. If all goes well, it will present you with a warning about permissions. Check yes and it will start the application.

Advertisements
This entry was posted in programming and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s