Скачать презентацию JAR FILES Deployment Packaging programs in Скачать презентацию JAR FILES Deployment Packaging programs in

5fe12a5fbe3a6bf16652e454ffe51c35.ppt

  • Количество слайдов: 33

JAR FILES JAR FILES

Deployment • Packaging programs in Jar files • Deployment applications with Java web start 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 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 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 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

Update a Jar file • jar –uf jar-file input-file(s) • jar uf Tic. Tac. Update a Jar file • jar –uf jar-file input-file(s) • jar uf Tic. Tac. Toe. jar images/new. gif • jar uf Tic. Tac. Toe. jar -C images new. gif

Run Jar-packaged software • <applet code=Tic. Tac. Toe. class width=120 height=120> </applet> • <applet Run Jar-packaged software • • 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 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 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 inputfile(s) • manifest-addition is the name Modify manifest file • jar cmf jar-file manifest-addition inputfile(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 UTF 8

Set application’s entry point • You provide this information with the Main-Class header 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: My. Package. My. Class • jar cfm My. Jar. jar Manifest. txt My. Package/*. class • java -jar My. Jar. jar

Add classes to the Jar file’s classpath • You specify classes to include 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: jar 1 -name jar 2 -name directory/jar 3 name • For example, We first create a text file named Manifest. txt with the following contents: Class-Path: My. Utils. jar • We then create a JAR file named My. Jar. jar by entering the following command:

Set package version information • We first create a text file named Manifest. txt 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: build 57 Implementation-Vendor: Sun Microsystems, Inc. • jar cmf My. Jar. jar Manifest. txt My. Package/*. class

Seal package within a Jar file • Packages within JAR files can be optionally 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: my. Company/first. Package/ Sealed: true Name: my. Company/second. Package/ Sealed: true • jar cmf My. Jar. jar Manifest. txt My. Package/*. class

Sign and verify Jar files • the signer signs the JAR file using a 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/Class. One. class SHA 1 -Digest: TD 1 GZt 8 G 11 d. XY 2 p 4 ol. SZPc 5 Rj 64= • 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 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 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 "abc 123". – the password for your alias is "mypass" • jarsigner -keystore mykeys -storepass abc 123 -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 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. Security. Exception: invalid SHA 1 signature file digest for

Jar-related APIs • • The java. util. jar package The java. net. Jar. URLConnection Jar-related APIs • • The java. util. jar package The java. net. Jar. URLConnection class The java. net. URLClass. Loader class Jar. Runner 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 Jar. Runner http: //www. xxx. yyy/Target. App. jar • Jar. Runner delegates most of the JAR-handling tasks to the Jar. Class. Loader class. Jar. Class. Loader extends the

Deployment • Packaging programs in Jar files • Deployment applications with Java web start 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 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

Launch Notepad •" src="https://present5.com/presentation/5fe12a5fbe3a6bf16652e454ffe51c35/image-22.jpg" alt="Run Java web start applications • From browser Launch Notepad •" /> Run Java web start applications • From browser Launch Notepad • 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 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 Launch Notepad

Read resource in a Jar file • // Get current classloader Class. Loader cl Read resource in a Jar file • // Get current classloader Class. Loader cl = this. get. Class(). get. Class. Loader(); // Create icons Icon save. Icon = new Image. Icon(cl. get. Resource("images/save. gif")); Icon cut. Icon = new Image. Icon(cl. get. Resource("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 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 Hello. World Applet • import javax. swing. JApplet; import java. awt. Graphics; public class Hello. World extends JApplet { public void paint(Graphics g) { g. draw. Rect(0, 0, get. Size(). width - 1, get. Size(). height - 1); g. draw. String("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 Using the paint method • public void paint(Graphics g) { //Draw a Rectangle around the applet's display area. g. draw. Rect(0, 0, get. Width() - 1, get. Height() - 1); //Draw the current string inside the rectangle. g. draw. String(buffer. to. String(), 5, 15); } • Applets inherit the paint method from the Abstract Window Toolkit (AWT) Container class

Handling events • import java. awt. event. Mouse. Listener; import java. awt. event. Mouse. Handling events • import java. awt. event. Mouse. Listener; import java. awt. event. Mouse. Event; public class Simple. Click extends Applet implements Mouse. Listener { public void init() { add. Mouse. Listener(this); } public void mouse. Clicked(Mouse. Event event) { add. Item("click!. . . "); } } • Applets inherit a group of event-handling methods from the Container class. The Container class defines several methods, such as process. Key. Event and process. Mouse. Event, for handling particular types of events, and then one catch-all method called process. Event.

Add UI components • Swing supplies the following UI components – Buttons (javax. swing. Add UI components • Swing supplies the following UI components – Buttons (javax. swing. JButton) – Checkboxes (javax. swing. JCheck. Box) – Single-line text fields (javax. swing. JText. Field) – Larger text display and editing areas (javax. swing. JText. Area) – Labels (javax. swing. JLabel) – Lists (javax. swing. JList) – Pop-ups (javax. swing. Popup) – Scrollbars (javax. swing. JScroll. Bar) – Sliders (javax. swing. JSlider) – Drawing areas (java. awt. Canvas) – Menus (javax. swing. JMenu, javax. swing. JMenu. Bar javax. swing. JMenu. Item, javax. swing. JCheck. Box. Menu. Item) – Containers (javax. swing. JPanel, javax. swing. JWindow and its subclasses)

Use UI components in applets • Because the JApplet class inherits from the AWT 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. – set. Layout - Sets the layout manager.

public class Scrolling. Simple extends JApplet { JText. Field field; public void init() { public class Scrolling. Simple extends JApplet { JText. Field field; public void init() { //Execute a job on the event-dispatching thread: //creating this applet's GUI. try { Swing. Utilities. invoke. And. Wait( new Runnable() { public void run() { create. GUI(); } }); } catch (Exception e) { } add. Item(false, "initializing. . . "); } private void create. GUI() { //Create the text field and make it uneditable. field = new JText. Field(); field. set. Editable(false); //Set the layout manager set. Layout(new java. awt. Grid. Layout(1, 0)); //Add the text field to the applet. add(field); }

Applet tag • <APPLET CODE=Applet. Subclass WIDTH=an. Int HEIGHT=an. Int> </APPLET> • <APPLET CODE=Simple. Applet tag •

var _app = navigator. app. Name; if (_app ==" src="https://present5.com/presentation/5fe12a5fbe3a6bf16652e454ffe51c35/image-33.jpg" alt=" •