|
Lesson 7: Packages and Java Archive File Format Until now, you have used classes from the Java API library by importing the package containing the class or classes you need. A package is a convenient way to organize groups of related classes, and in development, you should organize your application files into packages too. Packages make it easier to locate and use the class files and help you control access to class data at run time. When your application is fully tested, debugged, and ready for deployment, use the Java Archive file format to deploy the application. JAR file format is a compression and file packaging format and tool for bundling executable files with any other related application files so they can be deployed as one unit. This lesson shows you how to organize the program files from Part 2, Lesson 6: Internationalization into packages and deploy the executable and other related files to production using JAR file format. Normally, you would use packages from the beginning of development. Setting up Class PackagesIt is easy to organize class files into packages. All you do is put related class files in the same directory, give the directory a name that relates to the purpose of the classes, and add a line to the top of each class file that declares the package name, which is the same as the directory name where they reside. For example, the class and other related files for the program files from Part 2, Lesson 6: Internationalization can be divided into three groups of files: fruit order client, view order client, and server files. Although these three sets of classes are related to each other, they have different functions and are to be deployed separately. Create the DirectoriesTo organize the internationalization program into three packages, you could create the following three directories and move the listed source files into them:
Declare the Packages
Each As an example, the package declaration and import statements for the RMIClient2.java class file look like this:
Make Classes and Fields Accessible
With class files organized into packages, you have to declare
the server classes in the
So client programs can access the fruit order data, the fields of
the
Fields and methods without an access specifier such as
Here is the new DataOrder class.
Change Client Code to Find the Properties Files
In the example, the properties files (
The code that creates the messages = ResourceBundle.getBundle( "client2" + File.separatorChar + "MessagesBundle", currentLocale); Compile and Run the ExampleCompiling and running the example organized into packages is a little different from compiling and running the example in previous lessons. First, you have to execute the compiler and interpreter commands from one directory above the package directories, and second, you have to specify the package directories to the compiler and interpreter commands. Compile
These instructions assume development
occurs in the
Start rmi Registry:
Start the Server Unix:
Start RMIGermanApp
Here is the HTML code to load the German applet,
Note the directory/package name prefixed to
the applet class name (
To run the applet with appletviewer, invoke the HTML file from the directory just above client1 as follows:
cd /home/zelda/classes appletviewer rmiGapp.html Start RMIClient2 in French
Using JAR Files to DeployAfter testing and debugging, the best way to deploy the two client and server files is to bundle the executables and other related application files into three separate JAR files, one JAR file for each client program, and one JAR file for the server program. JAR files use the ZIP file format to compress and pack files into, and decompress and unpack files from, the JAR file. JAR files make it easy to deploy programs that consist of many files. Browsers can easily download applets bundled into JAR files, and the download goes much more quickly than if the applet and its related files were not bundled into a JAR file. Server Set of FilesHere are the server files:
Compress and Pack Server FilesTo compress and pack the server files into one JAR file, type the following command on one line. This command is executed in the same directory with the files. If you execute the command from a directory other than where the files are, you have to specify the full pathname.
jar is the jar command. If you
type jar with no options, you get the
following help screen. You can see from the help screen
that the cf options to the jar command
mean create a new
JAR file named server.jar and put the
list of files that follows into it. The new JAR file
is placed in the current directory.
To deploy the server files, all you have to do is move the server.jar file to a publicly accessible directory
on the server where they are to execute.
Decompress and Unpack Server Files
After moving the JAR file to its final location, the
compressed and packed files need to be decompressed and
unpacked so you can start the server. The following
command means extract ( jar xf server.jar Fruit Order Set of FilesThe fruit order set of files (below) consists of applet classes, web pages, translation files, and the policy file. Because they live on the web, they need to be in a directory accessible by the web server. The easiest way to deploy these files is to bundle them all into a JAR file and copy them to their location.
Compress and Pack Files
To deploy the fruit order client files, copy the applet.jar
file to its final location.
Decompress and Unpack Files
An applet in a JAR file can be invoked from an You can leave the translation files and policy file in the JAR file. When using appletviewer, the applet invoked from the JAR file will find them in the JAR file.
However, you do need to unpack the web pages so you can move them to their final location. The following command does this. Everything goes on one line. jar xv applet.jar index.html rmiEapp.html rmiFapp.html rmiGapp.html
View Order Set of FilesThe view order set of files (below) consists of the application class file and the policy file.
Compress and Pack Files
jar cf vieworder.jar RMIClient2.class java.policyTo deploy the view order client files, copy the vieworder.jar
file to its final location.
Decompress and Unpack Files
jar xf vieworder.jar More InformationYou can find more information on packages in the Creating and Using Packages lesson in The Java Tutorial. You can find more information on these and other JAR file format topics in the JAR File Format trail in The Java Tutorial. [TOP] | ||||||||||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||