User Tools

Site Tools


documentation:javanetworklaunchprotocol

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
documentation:javanetworklaunchprotocol [2010/02/15 11:17]
mcooper6
documentation:javanetworklaunchprotocol [2012/10/09 20:50] (current)
mcooper6 [Useful Links]
Line 1: Line 1:
 +<WRAP left 40%>
 +~~TOC~~
 +</​WRAP>​
 +//
 +//
 +<WRAP centeralign 100% bigger>
 +<WRAP bigger fgred>​Lab46 Tutorials</​WRAP>​
 +<WRAP muchbigger>​Java Network Launch Protocol</​WRAP>​
 +Up and Running
 +</​WRAP>​
 +<WRAP left 40%>
 +<WRAP right bgwhite>
 +<WRAP download round><​WRAP>​Java Web Start Demo</​WRAP>​[[http://​lab46.corning-cc.edu/​~mcooper6/​Payroll.jnlp|Launch It!]]</​WRAP>​
 +</​WRAP>​
 +</​WRAP>​
 +<WRAP clear></​WRAP>​
 +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 =====
 +<WRAP round info>
 +  *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.
 +</​WRAP>​
 +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 ===== 
 +<​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.
 +
 +<WRAP round info>
 +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.  ​
 +</​WRAP>​
 +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.
 +
 +<cli>
 +javac -classpath /​classes/​javaws.jar /​classes/​MyProgram.java
 +</​cli>​
 +
 +<WRAP info round>
 +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.
 +</​WRAP>​
 +
 +===== Create the Manifest =====
 +<​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:
 +
 +<​code>​
 +Main-Class: MyProgram ​
 +</​code>​
 +(this is the name of the class file created by the compiler, and it's case sensitive.) ​
 +
 +===== Archive the Class Files =====
 +<​progress=40>​
 +Now we can archive the classes with the following command:
 +
 +<cli>
 +jar cvfm MyProgram.jar /​classes/​MyProgram.mf /​classes/​*.class
 +</​cli>​
 +
 +===== The JNLP File =====
 +<​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:
 +<code xml>
 +<?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>​
 +</​code>​
 +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.
 +
 +<code xml>
 +<​resources>​
 + <j2se version="​1.5+"/>​
 + <jar href="​MyProgram.jar"/>​
 +</​resources>​
 +</​code>​
 +The codebase and href attributes of the <​jnlp>​ element are equally important (again, absolute URL's are best):
 +
 +<code xml>
 +<jnlp spec="​1.0+"​ codebase="​http://​www.mydomain.com/​mydirectory/"​ href="​MyProgram.jnlp">​
 +</​code>​
 +
 +If you intend to run this from the command line, you can change the <​jnlp>​ codebase to the following:
 +
 +<code xml>
 +<jnlp spec="​1.0+"​ codebase="​File:/​Classes/"​ href="​MyProgram.jnlp">​
 +</​code>​
 +
 +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 =====
 +<​progress=80>​
 +<cli>
 +javaws /​Classes/​Program.jnlp
 +</​cli>​
 +
 +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 =====
 +<​progress=100>​
 +Embed the following link to any HTML or PHP web page:
 +<code html>
 +<a href="​http://​www.mydomain.com/​mydirectory/​MyProgram.jnlp">​Launch It</​a>​
 +</​code>​
 +
 +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**.
documentation/javanetworklaunchprotocol.txt · Last modified: 2012/10/09 20:50 by mcooper6