Tag Archives: webstart

Installing a web server for a Java Webstart Deployment

The goal is to install a HTTP server on a linux box (debian) and place the files from a Java project in there to allow a deployment via Java Webstart. This is a reminder for myself, nothing that can’t be found by googling a little.

Install a HTTP server

I used apache as a server (it just works, there might be better options):

apt-get update
apt-get install apache2 apache2-doc apache2-utils

Check that the procedure worked by trying to access the server at http://hostnameOrIp, which should show a default web page.

That default page is located in /var/www, so you can start placing files in there as you wish.

Optional: make the folder available to Windows computers on the network via Samba:

Add the following profile to /etc/samba/smb.conf

[www]
   comment = Web Server
   path = /var/www
   guest ok = yes
   browseable = yes
   writeable = yes
   create mask = 0777
   directory mask = 0777

Change the permissions:

sudo chmod 777 -R /var/www

And restart Samba:

/etc/init.d/samba restart

Prepare the Java project files

I assume that the project is packaged as a JAR file, with a certain number of dependencies, available as JAR files too.

You can then sign and deploy the files to the server, with the associated JNLP file.

Prepare the index.html

A very basic html page containing a "launch" button:

<html><body>
    <script src= "http://www.java.com/js/deployJava.js"></script>
    <script>
        // using JavaScript to get location of JNLP
        // file relative to HTML page
        var dir = location.href.substring(0, location.href.lastIndexOf('/')+1);
        var url = dir + "YourJnlp.jnlp";
        deployJava.createWebStartLaunchButton(url);
    </script>
</body></html>
Advertisements
Tagged , ,

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 , ,