33
Deployment

Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Embed Size (px)

Citation preview

Page 1: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Deployment

Page 2: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Deployment

• Packaging programs in Jar files

• Deployment applications with Java web start

• Creating and deploying applets with Java Plug-in

Page 3: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Packaging programs in Jar files

• The Java Archive (JAR) file format enables you to bundle multiple files (classes and auxiliary resource) into a single archive file

• Security, decreased download time, compression, packaging for extension, package sealing, package versioning, portability

Page 4: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Outline

• Using Jar files

• Manifest files

• Signing and verifying Jar files

• Using Jar-related APIs

Page 5: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Using Jar filesCreate Jar file jar cf jar-file input-file(s)

View content jar tf jar-file

Extract content jar xf jar-file

Extract some files jar xf jar-file archived-file(s)

Run an application packaged as Jar file

jar –jar app.jar

Invoke applet packaged as Jar file

<applet code=AppletClassName.class archive="JarFileName.jar" width=width height=height> </applet>

Page 6: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Update a Jar file

• jar –uf jar-file input-file(s)

• jar uf TicTacToe.jar images/new.gif

• jar uf TicTacToe.jar -C images new.gif

Page 7: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Run Jar-packaged software

• <applet code=TicTacToe.class width=120 height=120>

</applet> • <applet code=TicTacToe.class

archive="TicTacToe.jar" width=120 height=120>

</applet>• java -jar jar-file • add a Main-Class header to the JAR file's

manifest: Main-Class: classname

Page 8: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Manifest files

• The manifest is a special file that can contain information about the files packaged in a JAR file– signing, – version control, – package sealing, – others

Page 9: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Default manifest file

• META-INF/MANIFEST.MF

• Manifest-Version: 1.0 Created-By: 1.5.0_01 (Sun Microsystems Inc.)

Page 10: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Modify manifest file

• jar cmf jar-file manifest-addition input-file(s)

• manifest-addition is the name (or path and name) of the existing text file whose contents you want included in the JAR file's manifest

• The m and f options must be in the same order as the corresponding arguments

• The contents of the manifest must be encoded in UTF8

Page 11: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Set application’s entry point• You provide this information with the Main-Class

header in the manifest, which has the general form:

Main-Class: classname

• The value classname is the name of the class that is your application's entry point

• For example, we first create a text file named Manifest.txt with the following contents:

Main-Class: MyPackage.MyClass

• jar cfm MyJar.jar Manifest.txt MyPackage/*.class

• java -jar MyJar.jar

Page 12: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Add classes to the Jar file’s classpath

• You specify classes to include in the Class-Path header field in the manifest file of an applet or application of the following form:

Class-Path: jar1-name jar2-name directory/jar3-name

• For example, We first create a text file named Manifest.txt with the following contents:

Class-Path: MyUtils.jar • We then create a JAR file named MyJar.jar by

entering the following command: jar cfm MyJar.jar Manifest.txt MyPackage/*.class

Page 13: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Set package version information• We first create a text file named Manifest.txt with the

following contents:

Name: java/util/ Specification-Title: Java Utility Classes Specification-Version: 1.2 Specification-Vendor: Sun Microsystems, Inc. Implementation-Title: java.util Implementation-Version: build57 Implementation-Vendor: Sun Microsystems, Inc.

• jar cmf MyJar.jar Manifest.txt MyPackage/*.class

Page 14: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Seal package within a Jar file• Packages within JAR files can be optionally sealed, which

means that all classes defined in that package must be archived in the same JAR file

• You might want to seal a package, for example, to ensure version consistency among the classes in your software

• We first create a text file named Manifest.txt with the following contents:

Name: myCompany/firstPackage/ Sealed: true Name: myCompany/secondPackage/ Sealed: true

• jar cmf MyJar.jar Manifest.txt MyPackage/*.class

Page 15: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Sign and verify Jar files• the signer signs the JAR file using a private key. • the corresponding public key is placed in the JAR file,

together with its certificate, so that it's available for use by anyone who wants to verify the signature.

• When you sign a JAR file, each file in the archive is given a digest entry in the archive's manifest: Name: test/classes/ClassOne.class SHA1-Digest: TD1GZt8G11dXY2p4olSZPc5Rj64=

• In addition to the signature file, a signature block file (.DSA) is automatically placed in the META-INF directory containing – the digital signature generated with signer’s private

key and – the certificate containing the signer’s public key

Page 16: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Sign Jar files• To sign a JAR file, you must first have a private key.

Private keys and their associated public-key certificates are stored in password-protected databases called keystores.

• A keystore can hold the keys of many potential signers. Each key in the keystore can be identified by an alias which is typically the name of the signer who owns the key

• jarsigner jar-file alias • This basic form of the command assumes that the keystore

to be used is in a file named .keystore in your home directory. It will create signature and signature block files with names x.SF and x.DSA respectively, where x is the first eight letters of the alias, all converted to upper case

Page 17: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Sign Jar files• For example:

– your alias is "johndoe".

– the keystore you want to use is in a file named "mykeys" in the current working directory.

– the keystore's password is "abc123".

– the password for your alias is "mypass"

• jarsigner -keystore mykeys -storepass abc123 -keypass mypass app.jar johndoe

• the .SF and .DSA files it creates would be named JOHNDOE.SF and JOHNDOE.DSA.

• Because the command doesn't use the -signedjar option, the resulting signed file will overwrite the original version of app.jar

Page 18: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Verify signed Jar files• jarsigner -verify jar-file • You'll see the following message if the verification is

successful: – jar verified.

• If you try to verify an unsigned JAR file, the following message results: – jar is unsigned. (signatures missing or not

parsable)

• If the verification fails, an appropriate message is displayed. For example, – jarsigner: java.lang.SecurityException: invalid

SHA1 signature file digest for test/classes/Manifest.class

Page 19: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Jar-related APIs• The java.util.jar package

• The java.net.JarURLConnection class

• The java.net.URLClassLoader class

• JarRunner enables you to run an application that's bundled in a JAR file by specifying the JAR file's URL on the command line. For example,

java JarRunner http://www.xxx.yyy/TargetApp.jar

• JarRunner delegates most of the JAR-handling tasks to the JarClassLoader class. JarClassLoader extends the java.net.URLClassLoader class

Page 20: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Deployment

• Packaging programs in Jar files

• Deployment applications with Java web start

• Creating and deploying applets with Java Plug-in

Page 21: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Java Web Start

• Java Web Start provides the power to launch full-featured applications with a single click. Users can download and launch applications, without installation procedures

• With Java Web Start, you can place a single Java application on a Web server for deployment to a wide variety of platforms

• Applications launched with Java Web Start are cached locally, for improved performance

Page 22: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Run Java web start applications

• From browser

<a href="Notepad.jnlp">Launch Notepad </a>

• You can run a Java Web Start application through the Java Application Cache Viewer

• Through the Java Application Cache Viewer, you can add a shortcut to the application to your desktop.

Page 23: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Deploy Java web start application• Set up web server

Configure the Web server so that files with the .jnlp extension are set to the application/x-java-jnlp-file MIME type

• Create the JNLP file<jnlp spec="1.0+“ codebase="http://java.sun.com/…/webstart/ex5/“ href="Notepad.jnlp"> <resources> <jar href="Notepad.jar"/>

<j2se version="1.3+“ href="http://java.sun.com/../j2se"/>

</resources> <application-desc main-class="Notepad"/> </jnlp>

• <a href="Notepad.jnlp">Launch Notepad Application</a>

Page 24: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Read resource in a Jar file• // Get current classloader

ClassLoader cl = this.getClass().getClassLoader(); // Create icons Icon saveIcon = new ImageIcon(cl.getResource("images/save.gif")); Icon cutIcon = new ImageIcon(cl.getResource("images/cut.gif"));

• The example assumes that the following entries exist in the JAR files for the application:

images/save.gif images/cut.gif

Page 25: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Deployment

• Packaging programs in Jar files

• Deployment applications with Java web start

• Creating and deploying applets with Java Plug-in

Page 26: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Applet • import javax.swing.JApplet;

import java.awt.Graphics; public class HelloWorld extends JApplet {

public void paint(Graphics g) { g.drawRect(0, 0, getSize().width - 1,

getSize().height - 1); g.drawString("Hello world!", 5, 15); } }

• import java.applet.Applet; import java.awt.Graphics; public class Simple extends Applet {

public void init() { } public void start() { } public void stop() { } public void destroy() { } public void paint(Graphics g) { } }

Page 27: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Using the paint method• public void paint(Graphics g) {

//Draw a Rectangle around the applet's display area.

g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);

//Draw the current string inside the rectangle. g.drawString(buffer.toString(), 5, 15);

}

• Applets inherit the paint method from the the Abstract Window Toolkit (AWT) Container class

Page 28: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Handling events• import java.awt.event.MouseListener;

import java.awt.event.MouseEvent; public class SimpleClick extends Applet

implements MouseListener {public void init() { addMouseListener(this); }public void mouseClicked(MouseEvent event)

{ addItem("click!... ");

} }

• Applets inherit a group of event-handling methods from the Container class. The Container class defines several methods, such as processKeyEvent and processMouseEvent, for handling particular types of events, and then one catch-all method called processEvent.

Page 29: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Add UI components• Swing supplies the following UI components

– Buttons (javax.swing.JButton) – Checkboxes (javax.swing.JCheckBox) – Single-line text fields (javax.swing.JTextField) – Larger text display and editing areas (javax.swing.JTextArea) – Labels (javax.swing.JLabel) – Lists (javax.swing.JList) – Pop-ups (javax.swing.Popup) – Scrollbars (javax.swing.JScrollBar) – Sliders (javax.swing.JSlider) – Drawing areas (java.awt.Canvas) – Menus (javax.swing.JMenu,javax.swing.JMenuBar

javax.swing.JMenuItem, javax.swing.JCheckBoxMenuItem)

– Containers (javax.swing.JPanel, javax.swing.JWindow and its subclasses)

Page 30: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Use UI components in applets

• Because the JApplet class inherits from the AWT Container class, it's easy to add components to applets and to use layout managers to control the components' onscreen positions. Here are some of the Container methods an applet can use: – add - Adds the specified Component.

– remove - Removes the specified Component.

– setLayout - Sets the layout manager.

Page 31: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

public class ScrollingSimple extends JApplet { JTextField field; public void init() {

//Execute a job on the event-dispatching thread: //creating this applet's GUI. try { SwingUtilities.invokeAndWait(

new Runnable() { public void run() { createGUI(); } });

} catch (Exception e) { } addItem(false, "initializing... "); }

private void createGUI() { //Create the text field and make it uneditable. field = new JTextField(); field.setEditable(false); //Set the layout manager setLayout(new java.awt.GridLayout(1,0)); //Add the text field to the applet. add(field); }

Page 32: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

Applet tag

• <APPLET CODE=AppletSubclass.class

WIDTH=anInt HEIGHT=anInt> </APPLET> • <APPLET CODE=Simple.class

CODEBASE="example/" WIDTH=500 HEIGHT=20> </APPLET>

• <APPLET CODE="AppletSubclass.class" ARCHIVE="file1, file2" WIDTH=anInt

HEIGHT=anInt> </APPLET>

Page 33: Deployment. Packaging programs in Jar files Deployment applications with Java web start Creating and deploying applets with Java Plug-in

• <html> <script language="Javascript"> var _app = navigator.appName; if (_app == 'Netscape') { document.write('<embed code="Applet1.class"', 'width="200"', 'height="200"', 'type="application/x-java-

applet;version=1.5.0">'); } else if (_app == 'Microsoft Internet Explorer') { document.write('<OBJECT ', 'classid="clsid:.….."', 'width="200"', 'height="200">',

‘<PARAM name="code" value="Applet1.class">',

'</OBJECT>'); } else { document.write('<p>unsupported

browser.</p>'); } </script>

</html>