Over a million developers have joined DZone.

Java Web Start (Jnlp) Hello World Example

DZone's Guide to

Java Web Start (Jnlp) Hello World Example

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

This tutorial shows you how to create a Java Web Start (Jnlp) file for user download.  When the user clicks on the downloaded jnlp file, it launches a simple AWT program. Here's the summary steps :

  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 it all into the Tomcat Folder
  5.     Access TestJnlp.jar from web through http://localhost:8080/Test.Jnlp

Before starting this tutorial, lets read this brief Java Web Start explanation from Oracle.

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.

Ok, let's go ~

1. Install JDk and Tomcat

Install Java JDK/JRE version above 1.5 and Tomcat.

2. Directory Structure

Directory structure of this example.



See the content of TestJnlp.java, it's just a simple AWT program with jnlp supported.

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.mkyong.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.*;" is not found, please include the jnlp library which islocated at JRE/lib/javaws.jar.

4. Jar It

Located your Java classes folder.  Jar it with following command in command prompt

jar -cf TestJnlp.jar *.*

This will package all the Java's classes into a new jar file, named "TestJnlp.jar".

5. Create keystore

Add a new keystore named "testkeys"

keytool -genkey -keystore testKeys -alias jdc

It will ask for a keystore password, first name, last name , organization's unit...etc..just fill them all.

6. Assign keystore to Jar file

Attached newly generated keystore "testkeys" to your "TestJnlp.jar" file

jarsigner -keystore testKeys TestJnlp.jar jdc

It will ask password for your newly created keystore

7. Deploy JAR it

Copy "TestJnlp.jar" to Tomcat's default web server folder, for example, in Widnows - C:\Program Files\Apache\Tomcat 6.0\webapps\ROOT


8. Create JNLP file

Create a new Test.jnlp file, content put this

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


9. Deploy JNLP file

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

10. Start Tomcat

Start Tomcat , C:\Tomcat folder\bin\tomcat6.exe

11. Test it

Access URL http://localhost:8080/Test.jnlp, it will prompt you to download the Test.jnlp file, just accept and double click on it.

If everything went fine, you should see the following output

Click on the "Run" button to lauch the AWT program.



If jnlp has no response, put the following code in your web.xml, which is located in the Tomcat conf folder.

<mime-mapping>   <extension>jnlp</extension>   <mime-type>application/x-java-jnlp-file</mime-type></mime-mapping>


Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}