Thursday, June 17, 2010

Java Web Start (Jnlp) Tutorial



Here is a brief explanation about Java Web Start from SUN
"Java Web Start is a mechanism for program delivery through a standard Web server. Typically initiated through the browser, these programs are deployed to the client and executed outside the scope of the browser. Once deployed, the programs do not need to be downloaded again, and they can automatically download updates on startup without requiring the user to go through the whole installation process again."
This Java Web Start (Jnlp) Tutorial – UnOfficial Guide will guide you to do following
1) Create a simple AWT program and jar it as TestJnlp.jar
2) Add keystore into TestJnlp.jar
3) Create a Jnlp file
4) Put all into Tomcat Folder
5) Access TestJnlp.jar from web through http://localhost:8080/Test.Jnlp

Tutorial

1. Install JDk and Tomcat

Install Java JDK/JRE version above 1.5 and Tomcat.

2. Create AWT file and file structure

Create file structure as following
TestJnlp.java
Create a new Java file, and put into the specified folder location, see above picture.
package com.mkyong;
 
import java.awt.*;
import javax.swing.*;
import java.net.*;
import javax.jnlp.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
 
public class TestJnlp {
static BasicService basicService = null;
public static void main(String args[]) {
JFrame frame = new JFrame("Mkyong Jnlp UnOfficial Guide");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel();
Container content = frame.getContentPane();
content.add(label, BorderLayout.CENTER);
String message = "Jnln Hello Word";
 
label.setText(message);
 
try {
basicService = (BasicService)
ServiceManager.lookup("javax.jnlp.BasicService");
} catch (UnavailableServiceException e) {
System.err.println("Lookup failed: " + e);
}
 
JButton button = new JButton("http://www.rgcollege.com");
 
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
try {
URL url = new URL(actionEvent.getActionCommand());
basicService.showDocument(url);
} catch (MalformedURLException ignored) {
}
}
};
 
button.addActionListener(listener);
 
content.add(button, BorderLayout.SOUTH);
frame.pack();
frame.show();
}
}
P.S If "import javax.jnlp.*;" not found, please include jnlp library which located at JRE/lib/javaws.jar.

3. Jar It

Located the Java's classes file folder and issue following command in command prompt
jar -cf TestJnlp.jar *.*
This is pack all the Java's classes into a new jar file , TestJnlp.jar.

4. Create keystore

Add a new keystore named "testkeys"
keytool -genkey -keystore testKeys -alias jdc
It will ask for keystore password, first name, last name , organization's unit…etc..just fill them all.

5. Assign keystore to Jar file

Attached newly keystore "testkeys" to TestJnlp.jar file
jarsigner -keystore testKeys TestJnlp.jar jdc
It will ask password for your newly created keystore

6. Deploy JAR it

Copy TestJnlp.jar to Tomcat's default web server folder.
C:\Program Files\Apache\Tomcat 6.0\webapps\ROOT

7. Create JNLP file

Create a new Test.jnlp file in order to execute TestJnlp.jar.
<?xml version="1.0" encoding="utf-8"?> 
<jnlp spec="1.0+" codebase="http://localhost:8080/" href="Test.jnlp">
<information>
<title>Jnlp Testing</title>
<vendor>YONG MOOK KIM</vendor>
<homepage href="http://localhost:8080/" />
<description>Testing Testing</description>
</information>
<security>
<all-permissions/>
</security>
<resources>
<j2se version="1.6+" />
<jar href="TestJnlp.jar" />
</resources>
<application-desc main-class="com.mkyong.TestJnlp" />
</jnlp>
Configuration is quite simple, please change accordingly.

8. Deploy JNLP file

Copy Test.jnlp to tomcat default web server folder also.
C:\Program Files\Apache\Tomcat 6.0\webapps\ROOT

9. Start Tomcat

C:\Tomcat folder\bin\tomcat6.exe
P.S Latest Tomcat like version 6, it already configure jnlp in web.xml properly. If jnlp is not response, please add following statement in your web.xml which located in tomcat conf folder.
  <mime-mapping>
<extension>jnlp</extension>
<mime-type>application/x-java-jnlp-file</mime-type>
</mime-mapping>

10. Test it

Access URL http://localhost:8080/Test.jnlp, download Test.jnlp and double click on it

11. Output

It will run as following if everything go fine
If you want to execute it again, just double click on Test.jnlp that you downloaded just now, it is no need to visit http://localhost:8080/Test.jnlp again

No comments:

Post a Comment