Return-Path: Received: from pacific-carrier-annex.mit.edu by po10.mit.edu (8.9.2/4.7) id SAA10481; Wed, 30 May 2001 18:29:05 -0400 (EDT) Received: from hermes.java.sun.com (hermes.java.sun.com [204.160.241.85]) by pacific-carrier-annex.mit.edu (8.9.2/8.9.2) with SMTP id SAA25779 for ; Wed, 30 May 2001 18:29:05 -0400 (EDT) Message-Id: <200105302229.SAA25779@pacific-carrier-annex.mit.edu> Date: Wed, 30 May 2001 15:29:05 PDT From: "JDC Tech Tips" To: alexp@mit.edu Subject: JDC Tech Tips May 30, 2001 Precedence: junk Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Beyond Email 2.2 J D C T E C H T I P S TIPS, TECHNIQUES, AND SAMPLE CODE WELCOME to the Java Developer Connection(sm) (JDC) Tech Tips, May 30, 2001. This issue covers the Java(tm) Network Launching Protocol (JNLP) and its reference implementation, Java(tm) Web Start. This tip was developed using Java 2 SDK, Standard Edition, v 1.3. This issue of the JDC Tech Tips is written by Stuart Halloway, a Java specialist at DevelopMentor (http://www.develop.com/java). You can view this issue of the Tech Tips on the Web at http://java.sun.com/jdc/JDCTechTips/2001/tt0530.html - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - THE JAVA NETWORK LAUNCHING PROTOCOL (JNLP) AND JAVA WEB START During the early days of the Java Programming Language, much emphasis was placed on client-side development. Support in the language for applets and for the secure downloading of code seemed ideal for delivering function over the World Wide Web. However the reality is that the Java Programming Language's greatest success has been on the server side. The language's power and flexibility has won the hearts and minds of server-side developers. Meanwhile, development on the client side has lagged. Tricky deployment problems have limited the utility of applets, and developers have been forced to turn to browser-based "thin" clients. The Java Network Launching Protocol (JNLP) promises to change all that. Developed through the Java Community Process as JSR-56, JNLP solves many of the previous problems in deploying Java function to the client. A JNLP client is an application or service that can launch applications from resources hosted on a network. If you package an application with JNLP, then a JNLP client can: o Detect, install, and use the correct version of the Java Runtime Environment for the application o Launch the application from the browser or the desktop o Automatically download newer versions of the application as they become available o Cache classes used by the application locally for fast startup o Run as either an applet or an application o Download native libraries if necessary o Use local resources, such as the filesystem, in a secure way o Automatically locate and load external dependencies Sun Microsystems provides a reference implementation of JNLP called Java Web Start. Let's use it to deploy a simple application that uses JFC Swing classes. To do this, you will need to download Java Web Start from http://java.sun.com/products/javawebstart. Here's the code for the application: //File HelloJNLP.java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class HelloJNLP extends JFrame { public HelloJNLP() { super("Hello JNLP"); String loadedFrom = this.getClass().getClassLoader().toString(); JLabel jl = new JLabel("loaded by " + loadedFrom); JEditorPane jtp = new JEditorPane("text/plain", "Edit this text"); getContentPane().add(jl, BorderLayout.NORTH); getContentPane().add(jtp, BorderLayout.CENTER); } public static void main(String [] args) { JFrame f = new HelloJNLP(); f.setBounds(100,100,325,250); f.setDefaultCloseOperation(DISPOSE_ON_CLOSE); f.setVisible(true); f.addWindowListener(new WindowAdapter() { public void windowClosed(WindowEvent e) { System.out.println("Shutting down..."); System.exit(0); } }); } } At the core of JNLP is a deployment manifest. The deployment manifest is an XML file with the extension .jnlp (the JNLP specification simply calls it the "JNLP file"). To deploy the HelloJNLP application, you need to describe it with a JNLP file, like this: Hello Tech Tips Sample May 2001 This manifest contains all the information that a client needs to download and use the HelloJNLP application: o The jnlp element's codebase attribute specifies the top-level URL to search for application resources. o The information element specifies information that a JNLP user interface can display to the client. o The j2se element specifies that the client must have version 1.2 or later of J2SE(tm). (This is a big improvement over applet deployment, which is often stuck with whatever VM the browser supplies.) o The jar element specifies the location of the application JAR file, relative to the jnlp codebase. o The application-desc element specifies the class to run. You can add subelements to specify command line arguments or system properties. To deploy the application to a web server, you need to execute the following steps: 1. Change the URLs in the jnlp codebase and href attributes to appropriate URLs for your web server. 2. Deploy the JNLP file to the web server. 3. Compile and JAR the HelloJNLP.java file, and deploy it to the web server. For example: jar cvf HelloJNLP.jar HelloJNLP.class HelloJNLP$1.class 4. Create an icon HelloJNLP.jpg and install it on the web server. You can use the one at http://staff.develop.com/halloway/TechTips/May2001/HelloJNLP.jpg 5. Set your web server's mime-type associations to map .jnlp to the mime-type application/x-java-jnlp-file. For example, with Apache, add the following to mime.types: application/x-java-jnlp-file jnlp 6. Restart the web server. To execute the application from a client machine, first make sure that you have installed Java Web Start. Then simply point your browser to the URL for the jnlp file. The Java Web Start client will download the jnlp file, download the necessary resources, and launch the application. What you should see is the text "Edit this text" displayed in an editor. If you are having trouble configuring the web server, or do not have access to a web server, you can also launch this application from http://staff.develop.com/halloway/TechTips/May2001/Hello.jnlp Notice that HelloJNLP is not running as an applet inside the browser, but as a separate, standalone application. When you close the application, HelloJNLP uses System.out to print the message "Shutting down..." However, no console is visible. The console is one of many settings that Java Web Start sets to "off" by default. This is one of a pair of settings that you should change, as follows: 1. Edit the javaws.cfg file in the Java Web Start install directory. Add the line "javaws.cfg.forceUpdate=true". This causes Java Web Start to automatically check for newer versions before starting an application. 2. Run the Java Web Start application. Under File->Preferences, go to the Advanced Tab and select "Show Java Console". Also, select "Log Output" and log output to a file of your choice. This is very helpful when you are debugging and need to catch System.out and System.err. The HelloJNLP application displays an editor, but the editor's contents are lost when you close the application. Add the following code to HelloJNLP to automatically save the editor state to the client hard drive: //changes to HelloJNLP.java import java.io.*; import java.net.*; import javax.jnlp.*; //replace the constructor with this new version: JEditorPane jtp; public HelloJNLP() { super("Hello JNLP, Second Version"); String loadedFrom = this.getClass().getClassLoader().toString(); JLabel jl = new JLabel("loaded by " + loadedFrom); jtp = new JEditorPane("text/plain", "Edit this text"); readEditorContents(); getContentPane().add(jl, BorderLayout.NORTH); getContentPane().add(jtp, BorderLayout.CENTER); addWindowListener(new WindowAdapter() { public void windowClosed(WindowEvent e) { System.out.println("Shutting down..."); try { writeEditorContents(); } catch (Exception ex) { System.out.println("Yoinks!"); ex.printStackTrace(); } System.exit(0); } }); } //add these helper methods private void writeEditorContents() throws UnavailableServiceException, IOException { System.out.println("writeEditorContents"); PersistenceService ps = (PersistenceService) ServiceManager.lookup("javax.jnlp.PersistenceService"); BasicService bs = (BasicService) ServiceManager.lookup("javax.jnlp.BasicService"); URL baseURL = bs.getCodeBase(); System.out.println("CodeBase was " + baseURL); URL editorURL = new URL(baseURL, "Editor"); try { ps.create(editorURL, 1024); } catch (Exception e) { e.printStackTrace(); } FileContents fc = ps.get(editorURL); DataOutputStream os = new DataOutputStream( fc.getOutputStream(false)); String s = jtp.getText(); os.writeUTF(s); os.flush(); os.close(); } private void readEditorContents() { try { PersistenceService ps = (PersistenceService) ServiceManager.lookup("javax.jnlp.PersistenceService"); BasicService bs = (BasicService) ServiceManager.lookup("javax.jnlp.BasicService"); URL baseURL = bs.getCodeBase(); URL editorURL = new URL(baseURL, "Editor"); FileContents fc = ps.get(editorURL); DataInputStream is = new DataInputStream(fc.getInputStream()); jtp.setText(is.readUTF()); is.close(); } catch (Exception e) { e.printStackTrace(); } } The JNLP API defines a set of services that bypass the security sandbox to enable some common client operations. In the writeEditorContents method, the BasicService discovers the application's codebase. Then, the PersistenceService caches the edit pane's contents on the local hard drive, keyed to a URL that is relative to the application's codebase. The name/value pairs provided by the PersistenceService are similar to browser cookies. The Java Web Start implementation honors this legacy by naming the pairs "muffins." Muffins are not appropriate for large data storage; they should be used to cache small identifiers on the client. These identifiers can then be used to locate larger pieces of information on the server. Redeploy the changed application to the web server, and try running it from the client -- again by browsing to the URL. If you do not have a web server, you can run this version from the URL http://staff.develop.com/halloway/TechTips/May2001/Hello2.jnlp Java Web Start automatically detects that that application has changed and runs the newer version. You can confirm this by checking the title bar string, which now should say "Hello JNLP, Second Version." Make some changes to the contents of the editor, and then close it. When you launch the application again, your changes should be visible. (You may see an exception in the console output the first time you run the new version. This is expected behavior because readEditorContents will not find a muffin the first time.) JNLP provides many more services than those shown here. For example, you can: o Finely control how applications are downloaded o Describe dependency relationships between JARs o Download and run native code installers o Grant additional permissions to signed code o Request specific versions of applications or applets To learn more about JNLP, download the JNLP specification at http://java.sun.com/products/javawebstart/download-spec.html To learn more about Java Web Start, see the Java Web Start page at http://java.sun.com/products/javawebstart/ . . . . . . . . . . . . . . . . . . . . . . . - NOTE Sun respects your online time and privacy. The Java Developer Connection mailing lists are used for internal Sun Microsystems(tm) purposes only. You have received this email because you elected to subscribe. To unsubscribe, go to the Subscriptions page (http://developer.java.sun.com/subscription/), uncheck the appropriate checkbox, and click the Update button. As of May 22, 2001, Sun Microsystems updated its Privacy Policy (http://sun.com/privacy) to give you a better understanding of Sun's Privacy Policy and Practice. If you have any questions, contact privacy@sun.com. - SUBSCRIBE To subscribe to a JDC newsletter mailing list, go to the Subscriptions page (http://developer.java.sun.com/subscription/), choose the newsletters you want to subscribe to, and click Update. - FEEDBACK Comments? Send your feedback on the JDC Tech Tips to: jdc-webmaster@sun.com - ARCHIVES You'll find the JDC Tech Tips archives at: http://java.sun.com/jdc/TechTips/index.html - COPYRIGHT Copyright 2001 Sun Microsystems, Inc. All rights reserved. 901 San Antonio Road, Palo Alto, California 94303 USA. This document is protected by copyright. For more information, see: http://java.sun.com/jdc/copyright.html - LINKS TO NON-SUN SITES The JDC Tech Tips may provide, or third parties may provide, links to other Internet sites or resources. Because Sun has no control over such sites and resources, You acknowledge and agree that Sun is not responsible for the availability of such external sites or resources, and does not endorse and is not responsible or liable for any Content, advertising, products, or other materials on or available from such sites or resources. Sun will not be responsible or liable, directly or indirectly, for any damage or loss caused or alleged to be caused by or in connection with use of or reliance on any such Content, goods or services available on or through any such site or resource. JDC Tech Tips May 30, 2001 Sun, Sun Microsystems, Java, and Java Developer Connection are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. To use our one-click unsubscribe facility, select the following URL: http://hermes.java.sun.com/unsubscribe?3838265397021680172