Title: Deployment
1Deployment
2Deployment
- Packaging programs in Jar files
- Deployment applications with Java web start
- Creating and deploying applets with Java Plug-in
3Packaging 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
4Outline
- Using Jar files
- Manifest files
- Signing and verifying Jar files
- Using Jar-related APIs
5Using 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 packaged as Jar file jar jar app.jar
Invoke applet packaged as Jar file ltapplet codeAppletClassName.class archive"JarFileName.jar" widthwidth heightheightgt lt/appletgt
6Update 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
7Run Jar-packaged software
- ltapplet codeTicTacToe.class
- width120 height120gt
- lt/appletgt
- ltapplet codeTicTacToe.class archive"TicTacToe.j
ar" - width120 height120gt
- lt/appletgt
- java -jar jar-file
- add a Main-Class header to the JAR file's
manifest Main-Class classname
8Manifest 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
9Default manifest file
- META-INF/MANIFEST.MF
- Manifest-Version 1.0
- Created-By 1.5.0_01 (Sun Microsystems Inc.)
10Modify 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
11Set applications 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
12Add classes to the Jar files 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
13Set 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
14Seal 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
15Sign 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 signers
private key and - the certificate containing the signers public key
16Sign 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
17Sign 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
18Verify 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/Manife
st.class
19Jar-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
20Deployment
- Packaging programs in Jar files
- Deployment applications with Java web start
- Creating and deploying applets with Java Plug-in
21Java 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
22Run Java web start applications
- From browser
- lta href"Notepad.jnlp"gtLaunch Notepad lt/agt
- 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.
23Deploy 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
- ltjnlp spec"1.0 codebase"http//java.sun.com/
/webstart/ex5/ href"Notepad.jnlp"gt - ltresourcesgt ltjar href"Notepad.jar"/gt
- ltj2se version"1.3
- href"http//java.sun.com/../j2se"/gt
lt/resourcesgt - ltapplication-desc main-class"Notepad"/gt lt/jnlpgt
- lta href"Notepad.jnlp"gtLaunch Notepad
Applicationlt/agt
24Read resource in a Jar file
- // Get current classloader
- ClassLoader cl this.getClass().getClassLoader()
- // Create icons
- Icon saveIcon new ImageIcon(cl.getResource("ima
ges/save.gif")) - Icon cutIcon new ImageIcon(cl.getResource("imag
es/cut.gif")) - The example assumes that the following entries
exist in the JAR files for the application - images/save.gif
- images/cut.gif
25Deployment
- Packaging programs in Jar files
- Deployment applications with Java web start
- Creating and deploying applets with Java Plug-in
26Applet
- 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)
27Using 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
28Handling 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.
29Add 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.JCheckBoxMenuIt
em) - Containers (javax.swing.JPanel,
javax.swing.JWindow and its subclasses)
30Use 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.
31- 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)
32Applet tag
- ltAPPLET CODEAppletSubclass.class WIDTHanInt
HEIGHTanIntgt lt/APPLETgt - ltAPPLET CODESimple.class CODEBASE"example/"
WIDTH500 HEIGHT20gt lt/APPLETgt - ltAPPLET CODE"AppletSubclass.class"
ARCHIVE"file1, file2" WIDTHanInt HEIGHTanIntgt
lt/APPLETgt
33- lthtmlgt
- ltscript language"Javascript"gt
- var _app navigator.appName
- if (_app 'Netscape')
- document.write('ltembed
code"Applet1.class"', - 'width"200"', 'height"200"',
- 'type"application/x-java-appletversion
1.5.0"gt') - else if (_app 'Microsoft Internet
Explorer') - document.write('ltOBJECT ',
'classid"clsid..."', - 'width"200"', 'height"200"gt',
- ltPARAM name"code" value"Applet1.class"gt',
-
'lt/OBJECTgt') - else document.write('ltpgtunsupported
browser.lt/pgt') - lt/scriptgt
- lt/htmlgt