33
Java Network Launch Protocol (JNLP) Tomas Bober

Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

  • View
    216

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Java Network Launch Protocol (JNLP)

Tomas Bober

Page 2: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

What it is

• A clever way to manage the contents of your java applications on a client’s computer.

• If you have an complicated application requiring resources to be downloaded to a client’s machine, this technology manages those resources.

• This technology ensures all users of your java applications always have the most up to date files to work from.

• This is done with almost no effort on the user’s end.

Page 3: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

How it works

• First time users of your application access the JNLP Server through the provided URL

• They allow a JRE plug-in to be installed on their system. This installs the client on the user’s machine.

• Anytime the resource files are updated on the web server, the update is passed down to the clients

• Users don’t worry about having the latest files since update are done automatically.

Page 4: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

More Details

• When a regular application is launched, the location of resources must already be known

• This is tough to do with a distributed application since a developer would need to specify all file paths on a user’s computer as well as web resources.

• But there is a better way: A launcher Program• This “launcher” program collects local and web

resources needed by the application and lets the JVM know where everything is.

• This way, when the application starts, all the resources are already available.

Page 5: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Pictorial Representation

Client Computer

JNLPClient

URL

JNLPServer

With resourcefiles

Web Resources

JVM

Resources

JavaApplication

Native Rsources

Page 6: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

How it works

• The JNLP file is actually an XML file • It uses the XML format to direct the JNLP Client to

retrieve resouce files, set security permissions, and how to handle the splash screen.

• The JNLP client executes the commands on the user’s machine and sets up the enviornment for the JVM.

• Very similar to batch files on a windows system.• JNLP files can be layered… one main and several

child layers can be used to obtain jar another other resources.

Page 7: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Advantages

• A rich client environment at runtime. Developers can use a new API to access system resources.

• A secure deployment solution. End users can trust a reliable JNLP Client and let that client manage additional files required by the application.

• Applications can evolve through incremental updates.

• Multiple JREs management and automatic installation of additional JREs and optional packages.

Page 8: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

More Advantages

• Offline operation. Deployed applications can work even without a connection with the deployment server.

• Automatic installation of native code/libraries. Some Java programs may need some platform-specific support for running.

• JNLP Client management console. • The capability of using platform-dependent

facilities such as shortcuts, accelerators, and utilities.

Page 9: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Disadvantages

• Users need to accept the client install• Client needs to run before the

application can run• Users see a splash screen before the

application starts

Page 10: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

An Example

• In this example we will setup the environment for using .jnlp files

• Go through the creation of the files needed to execute program with JNLP.

• Deploy the application locally• Deploy the application on NJITs AFS

server running Apache Tomcat

Page 11: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

The Application

• This example connects to United States National Institute of Standards and Technology and retrieves the current time

• It is a JFrame, not an applet, since it doesn’t extend applet and therefore can be compiled and executed from the command line

• Is not embedded in an html file

Page 12: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Write the basic app

• Compile and execute the following code locally on your computer through the command prompt

import java.awt.*;import javax.swing.*;import java.io.*;import java.net.*;

public class TheTime { public static void main(String args[]) { JFrame frame = new JFrame("Time Check"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel();

Page 13: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Code Part 2

Container content = frame.getContentPane(); content.add(label, BorderLayout.CENTER); String message = "missing"; BufferedReader reader = null; try { Socket socket = new Socket("time.nist.gov", 13); InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); reader = new BufferedReader(isr); reader.readLine(); // skip blank line message = reader.readLine();} catch (MalformedURLException e) { System.err.println("Malformed: " + e); } catch (IOException e) { System.err.println("I/O Exception: " + e);

Page 14: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Code Part 3

} finally { if (reader != null) {try { reader.close(); } catch (IOException ignored) { } } } label.setText(message); frame.pack(); frame.show(); }}

Page 15: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Writing the .jnlp

• Now that you know the code works, we will make it work with JNLP.

• Open a text editor and copy and paste the following code into it.

• Save it as a time.jnlp

Page 16: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

.jnlp Code Part 1

<?xml version="1.0" encoding="UTF-8"?><jnlp spec="1.0+" codebase="file:///c:/JNLP"><information> <title>Time Check</title> <vendor>NJIT - CS633</vendor> <homepage href="www.njit.edu" /> <description>JNLP Demo</description></information><offline-allowed/><security> <j2ee-application-client-permissions/>

Page 17: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

.jnlp Code Part 2

</security><resources> <j2se version="1.2+" /> <jar href="JNLPTime.jar"/></resources><application-desc main-class="TheTime" /></jnlp>

• The Codebase is the location of the .jar file we will create for this application

• The Information section contains the information the splash screen will display

Page 18: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

The .jnlp file

• Security tags are not always required, but here we set the security permissions to that of the j2ee environment on the client side.

• The resources section is where you describe what your application needs to run– We need to specify the j2se environment since that is

what will be used to run this application– We also specify the .jar file that contains the

application

Page 19: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

More on the .jnlp file

• We specify the main class as TheTime in the Application tag. This is the name of our compiled class. We would not need to do this if we specified one class in the jar file as the main class. This would be done if we used multiple jar files.

Page 20: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Create the JAR file

• First create the JAR filejar cf JNLPTime.jar TheTime.class • Create a key in the keystorekeytool -genkey -keystore myKeys -alias jdc

• Sign the JAR filejarsigner -keystore myKeys JNLPTime.jar jdc

Page 21: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Running the app locally

• To execute, just double click on time.jnlp

• You will see a screen like this.

• Click Run on the Security warning about the signed JAR file

• You will see the same output as before.

Page 22: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Running the app locally

• The time is going to be different in this output though.

• Here, the JNLP launcher opens the codebase directory, grabs whatever JAR file is specified in the resources, and executes it using the j2se environment specified.

• To update he app, we would just need to change the JAR file. Accessing the app would be exactly the same. The .jnlp file would automatically grab the updated JAR file and resources. It would then run using those rather then the old version.

Page 23: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Running the app on AFS

• First you need to ensure that Apache Tomcat is working correctly.

• In a browser type http://afs35.njit.edu/~userid• It can be afs31–35 since they’re allowed to run

the apache web server.• You should see your home page• You can double check Apache by going to the

page listed in the professor’s lecture notes.

Page 24: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Add a new MIME type

• Apache needs to know how to handle files of type .jnlp, but this feature is currently not setup in AFS.

• The normal way of resolving this is to add a new MIME type by opening the Apache Group\Apache\conf\mime.types file and inserting application/x-java-jnlp-file JNLP into the declarations.

• However, we as students, don’t have access to this file

Page 25: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

MIME workaround

• To get around this limitation, create a file called .htaccess in your public_html directory.

• Paste AddType application/x-java-jnlp-file .jnlp into the file

• This is another way to tell Apache how to handle the .jnlp files types without having access to the configuration files.

Page 26: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Changing the .jnlp

• You need to change the codebase in the .jnlp file to the location of the JAR files after you paste it to a AFS server.

• I changed mine to codebase="http://afs33.njit.edu/~trb2/JNLP"

• You need to specify the server you will be using. Meaning you need to write afs33.njit.edu rather then web.njit.edu

Page 27: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Running the .jnlp from AFS

• Copy and paste all the files to your public_html folder. I chose to put mine in a JNLP directory.

• To execute the program go to http://web.njit.edu/~yourid/JNLP/time.jnlp

• You will notice a new screen that looks like the following:

Page 28: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

New screen

• This screen launches the program on your local machine.

• If you check the box, you will not see this screen anymore and the .jnlp will launch automatically.

Page 29: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Automatic Updating

• If you check the “trust content from this publisher” on the singed JAR file screen, you won’t get the warning anymore

• You will notice that the .jnlp file is automatically updated. You can verify this by looking at the download folder using firefox.

• This updated file automatically grabs updated resources and executes the program.

Page 30: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Local Problems / Solutions

• If your program compiles and runs but does not execute using JNLP try one of the following:– Make sure you specify the code base properly.

Windows uses “C:\JNLP” while JNLP uses different slashes “file:///c:/JNLP”

– Make sure you spelled the name of the .jar file correctly

– Make sure you spelled the main-class correctly

Page 31: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Remote Problems-Solutions

• You only see the contents of the .jnlp file rather then it executing– Apache doesn’t know how to handle the .jnlp

file. Make sure you specified the new MIME type correctly.

– The .htaccess file needs to have the period in front of the “htaccess”

Page 32: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Remote Problems-Solutions

• The .jnlp file executes, but the application fails to launch– Make sure apache is working, if not, log ito the

SSH client and run “tomcat.setup” then wait a day.

– After installation, make sure you access your code from afs31-afs33 when you run Tomcat.

– Make sure you spelled the names and location of all resources correctly.

Page 33: Java Network Launch Protocol (JNLP) Tomas Bober. What it is A clever way to manage the contents of your java applications on a client’s computer. If you

Resources

• http://java.sun.com/docs/books/tutorial/deployment/webstart/deploying.html

• http://www.informit.com/articles/article.aspx?p=25043&rl=1

• http://www.informit.com/articles/article.aspx?p=25044&rl=1

• http://www.javascriptkit.com/howto/htaccess.shtml

• http://www.ufaq.org/navcom/mime_tutorial.html