Transcript deployment

Deployment
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
Outline
•
•
•
•
Using Jar files
Manifest files
Signing and verifying Jar files
Using Jar-related APIs
Using Jar files
Create 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 jar –jar app.jar
packaged as Jar file
<applet
Invoke applet
packaged as Jar file
code=AppletClassName.class
archive="JarFileName.jar"
width=width
height=height> </applet>
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
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
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
Default manifest file
• META-INF/MANIFEST.MF
• Manifest-Version: 1.0
Created-By: 1.5.0_01 (Sun
Microsystems Inc.)
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
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
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:
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
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
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
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
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
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
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
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 fullfeatured 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
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.
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
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
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) { } }
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
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.
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)
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.
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); }
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>
• <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>