Welcome to the Core Java Technologies Tech Tips, January 22, 2003. Here you'll get tips on using core Java technologies and APIs, such as those in Java 2 Platform, Standard Edition (J2SE). This issue covers:
These tips were developed using Java 2 SDK, Standard Edition, v 1.4. This issue of the Core Java Technologies Tech Tips is written by John Zukowski, president of JZ Ventures, Inc. READING FILES FROM JAVA ARCHIVES (JARS)Java archive (JAR) files are the standard way of packaging Java technology-based solutions. They allow a developer to package all relevant content (.class, image, sound, and support files) in a single file. The JAR format supports compression, authentication, and versioning, among many other features. Getting files out of JAR files can be a tricky task, but it doesn't have to be. This tip shows you how to get a file out of a JAR file, by first getting a directory of files in the JAR file, and then pulling out a specific one. If you are familiar with the popular ZIP format, JAR files aren't much different. JAR files provide a way of packaging multiple files into one, where each file may be compressed separately. What the JAR file adds is something called a manifest which allows a developer to provide additional information about the content. For example, the manifest can indicate which file in the JAR file to run to start an application, or the version of a library.
The Java 2 SDK, Standard Edition provides a jar tool that allows you to read and write JAR files from the console. However there might be times when you need to read and write JAR files from within your programs. (This tip will only cover reading JAR files from within a program.) The good news is that you can do this, and you don't have to worry about the decompression, because the library handles it for you. The classes you need are in the
To get started, you create a
JarFile jarFile = new JarFile("thefile.jar");
or
File file = new File("thefile.jar");
JarFile jarFile = new JarFile(file);
There are other constructors for authentication support and marking the file for deletion. However those constructors will not be covered here.
After you have a reference to the JAR file, you can read the directory of its contents. The entries method of
Enumeration enum = jarFile.entries();
while (enum.hasMoreElements()) {
process(enum.nextElement());
}
As previously mentioned, each individual entry is a Let's illustrate how to use these features in a program. The following program displays the name, size, and compressed size of the contents of a JAR file you specify. (This is similar to what the jar command does when you specify it with the "t" and "v" options.)
If you run the
Notice the META-INF lines at the start of the output. This is the manifest and security certificate information. The entries with a 0 size are not files, but rather directories.
To actually read a specific file from a JAR file, you must get the InputStream input = jarFile.getInputStream(entry);
After you have an input stream, you can just read it like any other stream. In the case of a text stream, remember to use a The following program demonstrates reading from a JAR file. Call the program with the name of a JAR file followed by the file name to read. The file to be read must have a text file type.
Suppose you had a text file named The itsy bitsy spider Ran up the water spout Down came the rain and Washed the spider out You could display the contents of the text file from the JAR file like this: java JarRead myfiles.jar spider.txt For more about JAR files, see the JAR file specification. GETTING STARTED WITH THE JAVA MANAGEMENT EXTENSIONS (JMX)The Java Management Extensions (JMX) offers a framework for the management and monitoring of resources. It is not yet a standard part of the Java 2 Standard Edition, but the reference implementation works with J2SE 1.4 and is a part of the Java 2 Enterprise Edition (J2EE) 1.4 specification currently in beta.
The purpose of JMX is to manage and monitor resources. Resources can be physical, for example small network devices, or logical, for example installed applications. You define the pieces in objects called This tip isn't meant to present a complete discussion of the how and why of the JMX architecture. Instead, it's designed to give you a quick, "hands-on" information about using JMX over the lifetime of a program. To get started, you need the binaries for the JMX version 1.2 reference implementation. You can download the binaries from the JMX home page. There is just one version for all platforms. Unpack the files into a working directory.
There are two JAR files in the download:
If your JRE directory is copy lib\*.jar \j2sdk1.4.1\jre\lib\ext
(If you don't want these available after this tip, remember to remove the files from the
Next, let's define an interface for the operations you want the
The first three methods look like they access JavaBean component properties (called "attributes" in JMX). In fact, they do access properties. The last method is simply a random method thrown in to show that everything defined in the interface doesn't have to be property-related. JMX supports executing any method, not just the getting and setting of properties. The implementation of this interface is rather simple:
Notice the naming convention of the concrete class. The interface name follows the name of
So far, you haven't seen any JMX-specific code, but that is about to change. The
The
MBeanServer server =
MBeanServerFactory.createMBeanServer();
Next, you register your
So, if you wanted to create and register two
For simplicity, the domain name here is just
A program with this much in it would be sufficient to support JMX. However, you wouldn't be able to see much. To see something more, you need a client interface. However instead of providing your own client interface, use the
Within
The complete server program follows, with explicit catching of all the possible exceptions:
You should now compile the three classes and run the javac HelloMBean.java Hello.java HelloAgent.java java HelloAgent
The
Your browser should display a screen that lists the
Clicking on one of the beans allows you to change or monitor its settings.
To demonstrate, try changing the message property and watch the change count increase.
Also clear the change count.
Clicking on the
There is much more to JMX than what this tip covered. This tip was designed to get you started so you can see how you might configure and monitor your Java solutions when they are deployed. Instead of developing custom solutions for exposing the management interface to the system configuration, simply use JMX. For more information about JMX, and for access to other JMX resources, visit the JMX home page. IMPORTANT: Please read our Terms of Use, Privacy, and Licensing policies: http://www.sun.com/share/text/termsofuse.html http://www.sun.com/privacy/ http://developer.java.sun.com/berkeley_license.html Comments? Send your feedback on the Core Java Technologies Tech Tips to: jdc-webmaster@sun.com Subscribe to other Java developer Tech Tips: - Enterprise Java Technologies Tech Tips. Get tips on using enterprise Java technologies and APIs, such as those in the Java 2 Platform, Enterprise Edition (J2EE). - Wireless Developer Tech Tips. Get tips on using wireless Java technologies and APIs, such as those in the Java 2 Platform, Micro Edition (J2ME). To subscribe to these and other JDC publications: - Go to the JDC Newsletters and Publications page, choose the newsletters you want to subscribe to and click "Update". - To unsubscribe, go to the subscriptions page, uncheck the appropriate checkbox, and click "Update". ARCHIVES: You'll find the Core Java Technologies Tech Tips archives at: http://java.sun.com/jdc/TechTips/index.html Copyright 2003 Sun Microsystems, Inc. All rights reserved. 901 San Antonio Road, Palo Alto, California 94303 USA. This document is protected by copyright. For more information, see: http://java.sun.com/jdc/copyright.html Sun, Sun Microsystems, Java, Java Developer Connection, JMX, J2SE, J2EE, and J2ME are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. | |||||||||||||||||||||
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.
|
| ||||||||||||