~~TOC~~ // // Lab46 Tutorials Java Network Launch Protocol Up and Running Java Web Start Demo[[http://lab46.corning-cc.edu/~mcooper6/Payroll.jnlp|Launch It!]] 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. ===== The Basics ===== *Create a Java file and import the javax.jnlp classes *Compile your .java file by setting the -classpath attribute to the path of javaws.jar (in your local JDK). *Create a manifest file to identify the main class of your program. *Archive your compiled Java classes into a .jar archive using a manifest file. *Create a .jnlp file to direct the web browser to your .jar archive *Put your .jnlp file and .jar archive on a server and link to the .jnlp file from a web page. 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. ===== Write and Compile ===== 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. ===== Create the Manifest ===== 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: Main-Class: MyProgram (this is the name of the class file created by the compiler, and it's case sensitive.) ===== Archive the Class Files ===== Now we can archive the classes with the following command: jar cvfm MyProgram.jar /classes/MyProgram.mf /classes/*.class ===== The JNLP File ===== 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: Save Demo Application Matt Cooper Web Start Save Demo This is nothing more than XML, but it's important to note the placement of the jar file reference within the 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. The codebase and href attributes of the element are equally important (again, absolute URL's are best): If you intend to run this from the command line, you can change the codebase to the following: 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: ===== Run It From the Command Line ===== javaws /Classes/Program.jnlp 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. ===== Link It ===== Embed the following link to any HTML or PHP web page: Launch It There you have it! Java JNLP, a quick and easy way to deploy Java applications on the web. ===== Useful Links ===== *[[http://java.sun.com/developer/technicalArticles/JavaLP/javawebstart/AutoInstall.html|Auto-Install: Easier Launching of Java Web Start Applications]] *[[http://java.sun.com/developer/JDCTechTips/2001/tt0530.html|JNLP and Java Web Start]] *[[http://java.sun.com/javase/technologies/desktop/javawebstart/index.jsp|Java SE Desktop Technologies]] *[[http://lopica.sourceforge.net/ref.html|JNLP Tag Reference]] *[[https://jdk6.dev.java.net/plugin2/jnlp/|JNLP Support in the New Java™ Plug-In Technology ]] *[[http://forums.sun.com/forum.jspa?forumID=38|Deploying - Java Web Start & JNLP (forum)]] *[[http://java.sun.com/javase/technologies/desktop/javawebstart/docs/developersguide.html|Java[tm] Web Start Developer's Guide]] *[[http://webstartfaq.com/|Java Web Start FAQ]] Do you have something to add to this article? Do your part, **send me a note at mcooper6 at corning-cc dot edu**.