Java Network Launch Protocol
Up and Running
Java Network Launch Protocol provides Java developers with new options for deploying applications across many different platforms. JNLP is intended to help developers eliminate platform dependencies by providing Java Runtime Environment management utilities that ensure proper compatibility as well as a few convenient tools aimed at making complex tasks very easy. Further, keeping Java Web Start applications up-to-date is a breeze, as they can be launched from a web server, ensuring that each time the latest version of the program is launched.
This document is not intended to introduce the Java programming language. It is instead aimed at those programmers who are looking to quickly test the capabilities of JNLP. Therefore, when I say begin by creating a Java file, I assume you know how to create a Java file. So, create your Java file.
<progress=10> Before you can utilize Java Web Start methods, you'll have to import javax.jnlp along with any other classes that your application needs. If you were to compile your program now (assuming you've written at least a main class), you would receive errors telling you that javax.jnlp does not exist. This is because, unlike classes that you normally import and then compile, JNLP requires a little support from the JDK. JNLP class files are actually archived in a jar file named javaws.jar. To compile JNLP files correctly, the -classpath option must be set to the location of this executable jar file.
In most installations of Java, the javaws.jar file is located at ~/usr/bin/jdkx.x.x_x/jre/lib, (~ represents the directory on your localhost where Java is installed and the x represents the version number of your installation). However, on Windows the Java JDK is often installed in the user's 'Program Files' directory, and this can cause a problem with the Window command line (due to the space). If that is your case (as it was mine), simply place a copy the javaws.jar file in a directory with no spaces in the name.
Lets assume we've created a new directory and named it 'classes', and that is where both our Java program and javaws.jar resides. To compile the class, use the -classpath option to the location of the javaws.jar copy.
javac -classpath /classes/javaws.jar /classes/MyProgram.java
NOTE: This demo uses methods contained within javaws.jnlp classes, therefore, the javaws.jnlp library has to be imported. However if you are only using Java Web Start to launch your program, the methods within the javaws.jnlp classes are not necessary and therefore do not have to be imported AND the resulting Java classes do not need to be compiled using the classes contained the javaws.jar.
<progress=30> There are a few more steps before take off. The new class files will need to be archived in a jar file themselves, and this means that a manifest file is needed. Create a new file (using the text editor of your choice) and give the file a '.mf' extension. Then, add at least the following to the file:
(this is the name of the class file created by the compiler, and it's case sensitive.)
<progress=40> Now we can archive the classes with the following command:
jar cvfm MyProgram.jar /classes/MyProgram.mf /classes/*.class
<progress=60> Are we there yet? Almost! Next, the web browser needs a way to tell Java Web Start that there's a jar file that needs to be accessed. To do this, we need to create a .jnlp file with, at minimum, the following:
<?xml version="1.0" encoding="utf-8"?> <jnlp spec="1.0+" codebase="http://www.mydomain.com/mydirectory/" href="MyProgram.jnlp"> <information> <title>Save Demo Application</title> <vendor>Matt Cooper</vendor> <description>Web Start Save Demo</description> <offline-allowed/> </information> <resources> <j2se version="1.5+"/> <jar href="MyProgram.jar"/> </resources> <application-desc/> </jnlp>
This is nothing more than XML, but it's important to note the placement of the jar file reference within the <resources> element which tells the Java JNLP engine where to look for the jar file to be launched. Use absolute URL's if your .jar file is not in the same directory as your .jnlp file.
<resources> <j2se version="1.5+"/> <jar href="MyProgram.jar"/> </resources>
The codebase and href attributes of the <jnlp> element are equally important (again, absolute URL's are best):
<jnlp spec="1.0+" codebase="http://www.mydomain.com/mydirectory/" href="MyProgram.jnlp">
If you intend to run this from the command line, you can change the <jnlp> codebase to the following:
<jnlp spec="1.0+" codebase="File:/Classes/" href="MyProgram.jnlp">
The codebase attribute tells Java Web Start where to find the .jar and .jnlp files that launch your Java application. In this case, I've placed my files on a web server. However, once you've created these files, you can run your application by calling the Java Web Start runtime from the command line. In this case, you'll need to use the second codebase, where '/Classes/' represents the directory on your localhost where you've placed your .jar archive. Use the following command to run your application from the command line:
Pretty neat! Now, to launch this on the web, simply place your .jnlp and your jar file in the same directory on your web server, then create a regular HTML file and link to your .jnlp file.
<progress=100> Embed the following link to any HTML or PHP web page:
There you have it! Java JNLP, a quick and easy way to deploy Java applications on the web.
Do you have something to add to this article? Do your part, send me a note at mcooper6 at corning-cc dot edu.