Running a Java application with Java Web Start

There are many tutorials out there that show how to deploy a simple application with Java Web Start. But when the project has more than one JAR it becomes a bit more complicated.

Netbeans

I first started with the automated option in Netbeans to launch the project with Java Web Start. Not only has it completely messed up with my customised Ant scripts, but I could not revert the changes easily ==> go back to pervious revision in the version control system.

The manual way

In summary, this is what is required, assuming that the application requires full rights to read / write files etc.

  • Compile the project
  • Sign each JAR file individually
  • Deploy the application

JAR Signing

Create a signature

In the main JDK folder / bin: run the keytool executable. I used:

 
keytool -genkeypair -dname "cn=YOUR NAME" -alias webstart -keypass a_password1 -storepass a_password2 -validity 365

That creates a keystore (called .keystore) in C:\Documents and Settings\user

Sign the JAR

The main JDK folder / bin has a executable that does that: jarsigner.
The basic syntax is (using the alias created above):

jarsigner MyJarFile.jar webstart

Batch signing

I then automated it using a new ant task to sign the main jar as well as all the libraries used by the project:

    <target name="signjar" depends="init">
        <echo level="info">Signing Jars</echo>
        <signjar
            alias="webstart" keystore=".keystore"
            storepass="a_password1"
            preservelastmodified="true">
            <path>
                <fileset dir="dist" includes="**/*.jar" />
            </path>
        </signjar>
    </target>

Deploying the application

Create the JNLP file

My JNLP (called test.jnlp) file looks like this. Note that all the jar in the lib folder need to be listed to avoid any security issues.
The “all-permissions” tag allow to access local resources (files, network etc.).

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" 
    codebase="http://mywebsite/jnlp" 
    href="http://mywebsite/jnlp/test.jnlp">
    <information>
        <title>Test JNLP</title>
        <vendor>A Vendor</vendor>
    </information>
    <resources>
        <!-- Application Resources -->
        <j2se version="1.7+" href="http://java.sun.com/products/autodl/j2se"/>

        <jar href="http://mywebsite/jnlp/Chart.jar" main="true" />
        <jar href="lib/joda-time-1.6.jar"/>
        <jar href="lib/opencsv-2.3.jar"/>
        <jar href="lib/jcommon-1.0.16.jar"/>
        <jar href="lib/appframework-1.0.3.jar"/>
        <jar href="lib/swing-worker-1.1.jar"/>
        <jar href="lib/blpapi3.jar"/>
        <jar href="lib/slf4j-api-1.6.1.jar"/>
        <jar href="lib/logback-classic-0.9.28.jar"/>
        <jar href="lib/logback-core-0.9.28.jar"/>
        <jar href="lib/substance.jar"/>
        <jar href="lib/trident.jar"/>
        <jar href="lib/vldocking-3.0.0.jar"/>
        <jar href="lib/ojdbc6.jar"/>
        <jar href="lib/JFreeChart.jar"/>
        <jar href="lib/swingx-beaninfo-1.6.2.jar"/>
        <jar href="lib/swingx-core-1.6.2.jar"/>
        <jar href="lib/commons-lang3-3.0-beta.jar"/>
        <jar href="lib/mysql-connector-java-5.1.17-bin.jar"/>
        <jar href="lib/commons-net-3.0.1.jar"/>
        <jar href="lib/bcprov-jdk16-146.jar"/>
        <jar href="lib/bcpg-jdk16-146.jar"/>
        <jar href="lib/mail.jar"/>
        <jar href="lib/commons-email-1.2.jar"/>
    </resources>
    <application-desc
         name="Test Web Start Application"
         main-class="com.wordpress.assylias.Charting"
         width="300"
         height="300">
     </application-desc>
     <update check="always"/>
    <security>
        <all-permissions/>
    </security>
</jnlp>

Excluding signed files

When launching the JNLP created above, one might get errors (typically: SHA1 or signature exceptions). The reason is that some of the libraries might already be signed, in which case they must not be signed a second time.
The solution is to release them as is, i.e. not signed, and amend the JNLP file as follows:

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" 
    codebase="http://mywebsite/jnlp" 
    href="http://mywebsite/jnlp/test.jnlp">
    <information>
        <title>Test JNLP</title>
        <vendor>A Vendor</vendor>
    </information>
    <resources>
        <!-- Application Resources -->
        <j2se version="1.7+" href="http://java.sun.com/products/autodl/j2se"/>

        <jar href="http://mywebsite/jnlp/Chart.jar" main="true" />
        <jar href="lib/joda-time-1.6.jar"/>
        <jar href="lib/opencsv-2.3.jar"/>
        <jar href="lib/jcommon-1.0.16.jar"/>
        <jar href="lib/appframework-1.0.3.jar"/>
        <jar href="lib/swing-worker-1.1.jar"/>
        <jar href="lib/blpapi3.jar"/>
        <jar href="lib/slf4j-api-1.6.1.jar"/>
        <jar href="lib/logback-classic-0.9.28.jar"/>
        <jar href="lib/logback-core-0.9.28.jar"/>
        <jar href="lib/substance.jar"/>
        <jar href="lib/trident.jar"/>
        <jar href="lib/vldocking-3.0.0.jar"/>
        <jar href="lib/ojdbc6.jar"/>
        <jar href="lib/JFreeChart.jar"/>
        <jar href="lib/swingx-beaninfo-1.6.2.jar"/>
        <jar href="lib/swingx-core-1.6.2.jar"/>
        <jar href="lib/commons-lang3-3.0-beta.jar"/>
        <jar href="lib/mysql-connector-java-5.1.17-bin.jar"/>
        <jar href="lib/commons-net-3.0.1.jar"/>
        <jar href="lib/mail.jar"/>
        <jar href="lib/commons-email-1.2.jar"/>

<!--        <jar href="lib/bcpg-jdk16-146.jar"/> -->
<!--        <jar href="lib/bcprov-jdk16-146.jar"/> -->

	<extension name="bcpg" href="bcpg.jnlp"/>
	<extension name="bcprov" href="bcprov.jnlp"/>
    </resources>
    <application-desc
         name="Test Web Start Application"
         main-class="com.wordpress.assylias.Charting"
         width="300"
         height="300">
     </application-desc>
     <update check="always"/>
    <security>
        <all-permissions/>
    </security>
</jnlp>

In this new version, 2 JAR which are already signed are extracted and included as extensions. It means that each of the 2 need their own JNLP. typically:

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="http://funds/jnlp" href="bcpg.jnlp">
    <information>
        <title>bcpg</title>
        <vendor>Sun Microsystems Inc</vendor>
    </information>
    <resources>
        <jar href="lib/bcpg-jdk16-146.jar"/>
    </resources>
    <component-desc/>
</jnlp>

Now to be honest to know which need to be included / excluded is a bit of a trial and error process and I have not found an automated way to do that.

Amend Ant Script

Finally I have amended the Ant script to reflect the 2 exclusions:

    <target name="signjar" depends="init">
        <echo level="info">Signing Jars</echo>
        <signjar
            alias="webstart" keystore=".keystore"
            storepass="a_password1"
            preservelastmodified="true">
            <path>
                <fileset dir="dist" includes="**/*.jar" excludes="**/bcpg-jdk16-146.jar **/bcprov-jdk16-146.jar" />
            </path>
        </signjar>
    </target>

And I have updated the deploy section to only deploy signed JARs:

    <target depends="init,signjar,check-deploy,do-deploy,dont-deploy" description="Deploy project." name="deploy"/>

And now it should work!

Troubleshooting

  • The jnlp page appears as text: you need to amend the HTTP server parameters. For example, for the Apache Web server you must add the following line to the .mime.types configuration file:
     application/x-java-jnlp-file JNLP 
Tagged , ,

2 thoughts on “Running a Java application with Java Web Start

  1. Leon Glass says:

    Thanks for this I like the post. I am having a problem with this process though and I wonder if you could help. I have an app that includes the jMonkey libs and my jnlp looks very similar to yours. My first issue is that web start does not seem to recognise the ear files lib directory where I have placed the jmonkey jars. I am using Eclipse but I have got the export script to include the jars in that directory. Differences with your jnlp 1: I have spec at 1.6+ 2: I dont have the href attribute of the j2s3 tag 3: I am not using any extensions all my jars are listed as resources.

    • assylias says:

      It is difficult to asy much without more information. I suggest you ask your question on a dedicated website, such as stackoverflow, with additional details (full jnlp, exact issue etc.). Best

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

%d bloggers like this: