Reading and Writing JAR Files

   

Reading and Writing JAR Files

The JDK 1.2 introduced several classes and packages to interact specifically with JAR files. These have been continued and also enhanced in the SDK 1.3. The classes and packages that deal with ZIP files are still in the core API as well.

The classes within the java.util.jar package can be used to read and write JAR files. Because the JAR file format is based on the standard ZIP file format, the package contains several classes that extend the classes found in the java.util.zip package.

Two other classes can be used in conjunction with reading and writing JAR files. They are java.net.URLClassLoader and java.net.JarURLConnection.

The java.net.JarURLClassLoader class is used to load classes and other resources from a search path of URL's that contain JAR files and directories. If a URL ends with a /, that URL is assumed to be a directory. Otherwise, it's assumed to be a JAR file that is opened and accessed accordingly . This class is helpful if you need to access a JAR file remotely or over a HTTP URL.

The java.net.JarURLConnection class extends java.net.URLConnection and provides a connection to a JAR file or an entry within the JAR file.

By using the previous classes and the classes within the java.util.jar package, you can imagine developing an application that is bundled completely as a JAR file. Using the manifest as stated earlier, you can specify the main class to start the application. Using the JarURLConnection class and the URLClassLoader, an application can reference an application bundled within a JAR and start that application. Suppose you have an application bundled within a JAR file called MyApp.jar. If the manifest file specifies the main class of the application, you could execute something like the following example:

 java ApplicationLoader http://www.host.com/MyApp.jar 

The ApplicationLoader class could get a connection to the remote JAR file and open up the JAR file for reading. The ApplicationLoader could then use the classes available in the java.util.jar package to determine the main class and call the main method on that class to start the application.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net