We now leave the world of applets and turn to the packaging of Java applications. When you ship an application, you don't want to deploy a mess of class files. Just as with applets, you should package the class files and other resources required by your program in a JAR file. Once the program is packaged, it can be loaded with a simple command or, if the operating system is configured appropriately, by double-clicking on the JAR file. The ManifestYou can package application programs, program components (sometimes called "beans" see Chapter 8 of Volume 2), and code libraries into JAR files. For example, the runtime library of the JDK is contained in a very large file rt.jar. A JAR file is simply a ZIP file that contains classes, other files that a program may need (such as icon images), and a manifest file that describes special features of the archive. The manifest file is called MANIFEST.MF and is located in a special META-INF subdirectory of the JAR file. The minimum legal manifest is quite boring: just Manifest-Version: 1.0 Complex manifests can have many more entries. The manifest entries are grouped into sections. The first section in the manifest is called the main section. It applies to the whole JAR file. Subsequent entries can specify properties of named entities such as individual files, packages, or URLs. Those entries must begin with a Name entry. Sections are separated by blank lines. For example,
To edit the manifest, place the lines that you want to add to the manifest into a text file. Then run
For example, to make a new JAR file with a manifest, run: jar cfm MyArchive.jar manifest.mf com/mycompany/mypkg/*.class To add items to the manifest of an existing JAR file, place the additions into a text file and use a command such as jar ufm MyArchive.jar manifest-additions.mf See http://java.sun.com/j2se/5.0/docs/guide/jar/jar.html for more information on the JAR and manifest file formats. Self-Running JAR FilesTo package an application, place all files that your application needs into a JAR file and then add a manifest entry that specifies the main class of your program the class that you would normally specify when invoking the java program launcher. Make a file, say, mainclass.mf, containing a line such as Main-Class: com/mycompany/mypkg/MainAppClass CAUTION
Do not add a .class extension to the main class name. Then run the jar command:
Users can now simply start the program as java -jar MyProgram.jar Depending on the operating system configuration, you may be able to launch the application by double-clicking on the JAR file icon.
ResourcesClasses that are used in both applets and applications often use associated data files, such as
In Java, such an associated file is called a resource. NOTE
For example, consider a class, AboutPanel, that displays a message such as the one in Figure 10-11. Figure 10-11. Displaying a resource from a JAR fileOf course, the book title and copyright year in the panel will change for the next edition of the book. To make it easy to track this change, we want to put the text inside a file and not hardcode it as a string. But where should you put a file such as about.txt? Of course, it would be convenient if you simply placed it with the rest of the program files, for example, in a JAR file. The class loader knows how to search for class files until it has located them somewhere on the class path, or in an archive, or on a web server. The resource mechanism gives you the same convenience for files that aren't class files. Here are the necessary steps:
The point is that the class loader remembers how to locate the class and it can then search for the associated resource in the same location. For example, to make an icon with the image file about.gif, do the following: URL url = AboutPanel.class.getResource("about.gif"); ImageIcon icon = new ImageIcon(url); That means "locate the about.gif file at the same place where you find AboutPanel.class." To read in the file about.txt, you can use similar commands: URL url = AboutPanel.class.getResource("about.txt"); InputStream stream = url.openStream(); Because this combination is so common, there is a convenient shortcut method: geTResourceAsStream returns an InputStream, not a URL. InputStream stream = AboutPanel.class.getResourceAsStream("about.txt"); To read from this stream, you need to know how to process input (see Chapter 12 for details). In the sample program, we read the stream a line at a time with the following instructions: InputStream stream = AboutPanel.class.getResourceAsStream("about.txt"); Scanner in = Scanner.create(stream); while (in.hasNext()) textArea.append(in.nextLine() + "\n"); The Core Java example files include a JAR file named ResourceTest.jar that contains all the class files for this example and the resource files about.gif and about.txt. This demonstrates that the program locates the resource file in the same location as the class file, namely, inside the JAR file. Example 10-12 shows the source code. TIP
Instead of placing a resource file inside the same directory as the class file, you can place it in a subdirectory. You can use a hierarchical resource name such as data/text/about.txt This is a relative resource name, and it is interpreted relative to the package of the class that is loading the resource. Note that you must always use the / separator, regardless of the directory separator on the system that actually stores the resource files. For example, on the Windows file system, the resource loader automatically translates / to \ separators. A resource name starting with a / is called an absolute resource name. It is located in the same way that a class inside a package would be located. For example, a resource /corejava/title.txt is located in the corejava directory (which may be a subdirectory of the class path, inside a JAR file, or, for applets, on a web server). Automating the loading of files is all that the resource loading feature does. There are no standard methods for interpreting the contents of a resource file. Each program must have its own way of interpreting the contents of its resource files. Another common application of resources is the internationalization of programs. Language-dependent strings, such as messages and user interface labels, are stored in resource files, with one file for each language. The internationalization API, which is discussed in Chapter 10 of Volume 2, supports a standard method for organizing and accessing these localization files. Example 10-12. ResourceTest.java1. import java.awt.*; 2. import java.awt.event.*; 3. import java.io.*; 4. import java.net.*; 5. import java.util.*; 6. import javax.swing.*; 7. 8. public class ResourceTest 9. { 10. public static void main(String[] args) 11. { 12. ResourceTestFrame frame = new ResourceTestFrame(); 13. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 14. frame.setVisible(true); 15. } 16. } 17. 18. /** 19. A frame with a panel that has components demonstrating 20. resource access from a JAR file. 21. */ 22. class ResourceTestFrame extends JFrame 23. { 24. public ResourceTestFrame() 25. { 26. setTitle("ResourceTest"); 27. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 28. add(new AboutPanel()); 29. } 30. 31. public static final int DEFAULT_WIDTH = 300; 32. public static final int DEFAULT_HEIGHT = 300; 33. } 34. 35. /** 36. A panel with a text area and an "About" button. Pressing 37. the button fills the text area with text from a resource. 38. */ 39. class AboutPanel extends JPanel 40. { 41. public AboutPanel() 42. { 43. setLayout(new BorderLayout()); 44. 45. // add text area 46. textArea = new JTextArea(); 47. add(new JScrollPane(textArea), BorderLayout.CENTER); 48. 49. // add About button 50. URL aboutURL = AboutPanel.class.getResource("about.gif"); 51. JButton aboutButton = new JButton("About", new ImageIcon(aboutURL)); 52. aboutButton.addActionListener(new AboutAction()); 53. add(aboutButton, BorderLayout.SOUTH); 54. } 55. 56. private JTextArea textArea; 57. 58. private class AboutAction implements ActionListener 59. { 60. public void actionPerformed(ActionEvent event) 61. { 62. InputStream stream = AboutPanel.class.getResourceAsStream("about.txt"); 63. Scanner in = new Scanner(stream); 64. while (in.hasNext()) 65. textArea.append(in.nextLine() + "\n"); 66. } 67. } 68. } java.lang.Class 1.0
SealingWe mentioned in Chapter 4 that you can seal a Java language package to ensure that no further classes can add themselves to it. You would want to seal a package if you use package-visible classes, methods, and fields in your code. Without sealing, other classes can place themselves into the same package and thereby gain access to its package-visible features. For example, if you seal the package com.mycompany.util, then no class outside the sealed archive can be defined with the statement package com.mycompany.util; To achieve this, you put all classes of the package into a JAR file. By default, packages in a JAR file are not sealed. You can change that global default by placing the line Sealed: true into the main section of the manifest. For each individual package, you can specify whether you want the package sealed or not, by adding another section to the JAR file manifest, like this: Name: com/mycompany/util/ Sealed: true Name: com/mycompany/misc/ Sealed: false To seal a package, make a text file with the manifest instructions. Then run the jar command in the usual way:
|