Skip navigation.

Sign up | Lost password? | Help

Mr Green's Coffeshop

Heal the world, smoke weed!

Posts tagged with "Programming"

Java Web Start

,

Java Web Start allows you to launch a Java application by clicking on a web page link. The application is cached locally and every time the application runs, Java Web Start checks if there is a newer version on the server, and if so the new version is downloaded.

I decided to test it out with a small test application that just shows an empty JFrame, JWSTest.java:
package jwstest;
import javax.swing.JFrame;
public class JWSTest {
    public static void main(String[] args)
    {
        JFrame f = new JFrame();
        f.setSize(100, 100);
        f.setTitle("TestFrame");
        f.setVisible(true);
    }
}


Netbeans creates the Jar file for me so I won't go through that.


Place the Jar file on your web server and create JWSTest.jnlp file in the same location:
<?xml version="1.0" encoding="utf-8"?>
        <jnlp spec="1.0"
                codebase="http://my.opera.com/Mr%20Green/homes/dist"
                href="JWSTest.jnlp">
        <information>
                <title>TestFrame Demo</title>
                <vendor>GreenSoft AS</vendor>
                <offline-allowed/>
        </information>
        <resources>
                <jar href="JWSTest.jar"/>
                <j2se version="1.3+"
                href="http://java.sun.com/products/autodl/j2se"/>
        </resources>
        <application-desc main-class="jwstest.JWSTest"/>
</jnlp>


Now you can direct Opera to http://my.opera.com/Mr%20Green/homes/dist/JWSTest.jnlp to launch the application. This runs in the sandbox so you don't need to fear havoc by clicking the link. You need Java installed.

Netbeans / Matisse layout manager

Now if you try to do the same with a jar file from an existing project created in Netbeans, and try to run it via JWS it just refuses to load. It seems like the application starts but never gets displayed, the javaw process is running but no application is visible.

This has to do with the swing-layout-1.0.jar file used when building GUI's with Matisse/Netbeans. I must be included as a reference in the jnlp file and placed on the server or the application never shows.

For more information on Netbeans/JWS look below.

To enable Java Web Start for Netbeans applications go here:
http://www.netbeans.org/kb/articles/matisse-jaws.html


Security / All permissions

By default, applications launched via JWS runs in a sandbox environment, just lika an Applet would. As an example you won't be able to write to the file-system or create connections to hosts other than the one where the application came from. If you want your app to have the same permissions as a "native/local" java application all the JAR files in the application needs to be signed. Even the swing-layout-1.0.jar file and other third party libraries must be signed. See my previous post on "Signing jar files"

After signing the Jar files you have to insert the following into the jnlp file to actually get all permissions:
<security>
 <all-permissions/>
</security>


If the following dialog box appears you've successfully signed your application:


The warning says the certificate is invalid since it is not from a Certificate Authority. Since that costs money I won't bother with that.



One issue that I haven't figured out yet is why the javaw process never ends. After shutting down an application started with Java Web Start, the javaw process is still running. If you start the same application 10 times there will be 10 javaw processes running after they have all been closed/exited.

Signing JAR files.

, ,

In order to get access to the filesystem all jar files in an application must be signed if it is started via Java Web Start or if it is an Applet.

First step is generating a keypair with the keytool.

/usr/lib/j2sdk1.5-sun/bin/keytool -genkey -alias green -keypass greenpass


green is the alias for the keypair, and greenpass is the password to access that alias(or keypair). The keypair will be stored in the file .keystore in your homedirectory unless you specify otherwise.

You'll be asked a lot of questions, you may lie or just hit enter!

Create a self-signed certificate:
/usr/lib/j2sdk1.5-sun/bin/keytool -selfcert -alias green



After the keypair has been genereated the jar file can be signed with jarsigner:

In it's easiest form, jarsigner's syntax:
jarsigner jarfile alias

/usr/lib/j2sdk1.5-sun/bin/jarsigner JWSTest.jar green


It will ask for both passwords specified while generating the keys.

The jar file has now been signed ;)
December 2009
M T W T F S S
November 2009January 2010
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31