Java Web Start (jnlp) simple example
One of my current projects requires me to deliver a Swing application via Java Web Start. This meant some research to figure out how that works. Since i’m a nice guy, here’s the result for everyone that needs it.
As you can see – in the folder structure image – there are some files that are required for the whole thing to work. Here’s a brief explanation about each of them (in my creation order
):
- src/org/Test.java : My source code. It’s just a simple main that creates a JFrame and displays it;
- Manifest.txt : The manifest file used to create the jar file that gets served by Java Web Start;
- faren.jar : My application’s jar. I’ll explain how to create it in a while;
- faren.jnlp : The Java Web Start description file;
- faren.html : The html file that contains the button to start the application.
The first thing we want to do is create the source file. In the case of my simple test, here are its contents (blank lines removed for brevity):
Test.java
package org;
import javax.swing.JFrame;
import java.awt.Dimension;
public class Test {
public Test() {
JFrame faren = new JFrame("Test");
faren.setSize(new Dimension(300, 300));
faren.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
faren.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
Moving on to the jar manifest file, it basically just says what the main class will be:
Manifest.txt
Main-Class: org.Test
Creating the jar is as simple as running the following command:
jar cfm faren.jar Manifest.txt org
It creates the faren.jar file, using the manifest file and the sources contained within the org folder. If – like me – you use Maven, you will have this for free using the app-assembler plugin, but for the test’s purposes i didn’t want to create a full maven project
Now we need to define a description file for the Java Web Start mechanism:
faren.jnlp
<?xml version="1.0" encoding="UTF-8"?> <jnlp spec="1.0+" codebase="file:///Users/pedroassuncao/Desktop/jnlp_example"> <information> <title>Dynamic Tree Demo</title> <vendor>Dynamic Team</vendor> </information> <resources> <!-- Application Resources --> <j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se"/> <jar href="faren.jar" main="true" /> </resources> <application-desc name="Dynamic Tree Demo Application" main-class="org.Test" width="300" height="300"> </application-desc> <update check="background"/> </jnlp>
Just to explain a little bit of what is going on in here (the important parts), what you want to make sure is that the codebase property is set correctly to the absolute path where the html file will be served from (in the case of a final deployment this will be something like http://myserver/jnlp/myapp, but in this case i’m serving without a web server, hence the file:/// part). You will also want to define a minimum version for the virtual machine of the client (the 1.6+ part) and eventually extend this information with the <shortcut> tag, that allows you to ask web start to create a desktop icon for your application on the clients’ computers. A full description of the structure of this file can be found here.
Finally, we need an html file that will show the java web start button. The following will suffice for now:
faren.html
<html>
<head></head>
<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 + "faren.jnlp";
deployJava.createWebStartLaunchButton(url, '1.6.0');
</script>
<noscript>
<a href="faren.jnlp">Launch</a>
</noscript>
</body>
</html>
If the client’s browser supports javascript, this will use a special script from the java website that will ask to install the virtual machine if the client does not have it (or upgrade an existing one). If he doesn’t support javascript it will just show a link that will try to launch the java web start (depending a lot on whether the user’s file associations are correct (i.e. .jnlp files being assigned to the java executable).
Finally, with all this in place, point your browser to the place where you have the faren.html file and click the button; the application should launch.
By the way, does anyone know why my desktop shortcut is not being added on OSX?
Related:
- Count (group by) number of line occurrences in file Here’s a neat ruby script to group and count the...
- Apple will remove java from OSX with Lion, so what? I don’t get all the fuss about Apple no longer...
Categorised as: computers, experiments, software development, tips, tutorials

good
as you say
you are really a nice guiy
thank you
Hello, Pedro thanks for this great tutorial. But I have a problem with reading app.properties file by using jnlp. How should i include this app.properties to jnlp so that we can correctly reference to that file. My project cannot find app.properties when i am using java web start.
Hi Emrah, unfortunately i'm no longer using jnlp (fear of Oracle abandoning it), so i can't really be of much help
Best of luck,–Pedro Assunção<p style=”color: #A0A0A8;”>
Thanks, Pedro. This was very easy to understand, and it’s exactly what I was looking for. Also, I really like the format of your resume. May your chute always open!
Wow, that's probably the best comment ever. Thanks a lot
This was easily the best example I could find of a basic web start application. I keep having trouble trying to launch my program because I get a java.io.FilePermission error whenever my code tries to load an image. Any thoughts/could you maybe post a more detailed tutorial involving actual swing components and loaded resources? Thanks.
How are you trying to load that image? From the classpath? Keep in mind that there are heavy restrictions on IO from the webstart's point of view
Finest kind! Thanks for this concise and complete example.
No problem, anytime.
Extremely neat article, took me exactly five minutes to get it running, Thanks again for the Brevity
My pleasure