2009年3月4日 星期三

Write Java WebStart Application in 5 minutes

They say, the best way of learning is to do-it-yourself. When I was assigned to write an article on Java Webstart, I thought to truly understand what's happening with Java Web Start, let's create a program and try to deploy it through any web server.

Pre-requisities

  1. You need to have the latest version of JDK installed on your system before you can proceed. We use JDK 1.6. Here is a guide to install JDK 1.6 / Java SE 6 in Linux under 5 minutes.
  2. You will ideally need a source code editor like JEdit. At the least use notepad or vi (on *nix) to edit the source code. Do not use any word processing programs like Microsoft Word or Open Office Writer.
  3. Create a Java file that you want to run using JNLP. This can be a regular Swing or AWT based application. It can even be a console application if you wish.
  4. Compile and test your source code(s) as you would with a regular program.

Create a Jar file

Java Web Start applications need to be packaged within a jar file. You can create a .jar file from your class file(s) using this command (assume you class file is named Test.class):

   jar cvf test.jar Test.class
Note: v is for verbose mode. You can omit it if you want.

Create a key

You need to create a keystore which will then be used to sign the jar files:

      keytool -genkey -keystore myKeys -alias jdc

When prompted, fill in your name. You can skip the other details if you want.

Note: myKeys is the name of your keystore. You will need it in the next step to sign the jar file(s).

Now let's sign the jar file(s)

Run the command below to sign the jar file. When prompted for password, give it a six letter, easy to remember (just for demo not for production) password.

 jarsigner -keystore myKeys *.jar jdc

Create a JNLP file

jnlp

A JNLP file provides the specification for your Java Web Start application. We will not cover the details of a JNLP file in this brief guide. However we will provide you with a simple jnlp file which you can use for many occassions. JNLP file is in XML format. Use a source code editor to edit the file. Let's save it as test.jnlp on the document root of your local web server. You can use JWS, a simple web server that ships with Java Web Start, Apache, Tomcat or any other web server. The web server itself doesn't need any special capability to serve Java Web Start applications.

Note:

1. The main-class (test) is the main class of your application
2. We have used to allow the JNLP application to access all client resource. This is not something you may want to do in production environment. In production you should only ask for permissions that you really need to give customers peace of mind. Check the documentation of j2ee-application-client-permissions for details.

Create html file and run the baby!

Create a html file in the same location as the other files containing just the link to your jnlp file on your web server like say http://localhost/test.jnlp in this example. Now gather around your peers and boss (if you are feeling adventurous) and open the html file you just created in any browser. You will be prompted to open it in Java Web Start. Agree to it and also agree to any subsequent message asking you to grant permission to the application.

That's it guys! Wasn't that easy?


source: http://blog.taragana.com/

沒有留言: