Certification Objective JAR Files (Objective 7.5)


Certification Objective —JAR Files (Objective 7.5)

7.5 Given the fully-qualified name of a class that is deployed inside and/or outside a JAR file, construct the appropriate directory structure for that class. Given a code example and a classpath, determine whether the classpath will allow the code to compile successfully.

JAR Files and Searching

Once you've built and tested your application, you might want to bundle it up so that it's easy to distribute and easy for other people to install. One mechanism that Java provides for these purposes is a JAR file. JAR stands for Java Archive. JAR files are used to compress data (similar to ZIP files) and to archive data.

Let's say you've got an application that uses many different classes that are located in several different packages. Here's a partial directory tree:

 test    |    |--UseStuff.java    |--Ws        |        |--(create MyJar.jar here)        |--myApp              |              |--utils              |      |              |      |--Dates.class       (package myApp.utils;)              |      |--conversions.class    "          "              |              |--engine                     |                     |--rete.class        (package myApp.engine;)                     |--minmax.class         "          " 

You can create a single JAR file that contains all of the class files in myApp, and also maintains myApp's directory structure. Once this JAR file is created, it can be moved from place to place, and from machine to machine, and all of the classes in the JAR file can be accessed via classpaths and used by java and javac. All of this can happen without ever unJARing the JAR file. Although you won't need to know how to make JAR files for the exam, let's make the current directory ws, and then make a JAR file called MyJar.jar:

 cd ws jar -cf MyJar.jar myApp 

The jar command will create a JAR file called MyJar.jar and it will contain the myApp directory and myApp's entire subdirectory tree and files. You can look at the contents of the JAR file with the next command (this isn't on the exam either):

 jar -tf MyJar.jar 

which will list the JAR's contents something like this:

 META-INF/ META-INF/MANIFEST.MF myApp/ myApp/.DS_Store myApp/utils/ myApp/utils/Dates.class myApp/utils/Conversions.class myApp/engine/ myApp/engine/rete.class myApp/engine/minmax.class 

Okay, now back to exam stuff. Finding a JAR file using a classpath is similar to finding a package file in a classpath. The difference is that when you specify a path for a JAR file, you must include the name of the JAR file at the end of the path. Let's say you want to compile UseStuff.java in the test directory, and UseStuff.java needs access to a class contained in myApp.jar. To compile UseStuff.java you would say

 cd test javac -classpath ws/myApp.jar UseStuff.java 

Compare the use of the JAR file to using a class in a package. If Usestuf f.java needed to use classes in the myApp.utils package, and the class was not in a JAR, you would say

 cd test javac -classpath ws UseStuff.java 

Remember when using a classpath, the last directory in the path must be the super-directory of the root directory for the package. (In the preceding example, myApp is the root directory of the package myApp.utils.) Notice that myApp can be the root directory for more than one package (myApp.utils and myApp.engine), and the java and javac commands can find what they need across multiple peer packages like this. In other words, if ws is on the classpath and ws is the super-directory of myApp, then classes in both the myApp.utils and myApp.engine packages will be found.

image from book
Exam Watch

When you use an import statement you are declaring only one package. When you say import java. util. * ; you are saying "Use the short name for all of the classes in the java.util package." You're NOT getting the java.util.jar classes or java.util.regex packages! Those packages are totally independent of each other; the only thing they share is the same "root" directory, but they are not the same packages. As a corollary, you can't say import java.*; in the hopes of importing multiple packages —just remember, an import statement can import only a single package.

image from book

Using /jre/lib/ext with JAR Files

When you install Java, you end up with a huge directory tree of Java-related stuff, including the JAR files that contain the classes that come standard with J2SE. As we discussed earlier, java and javac have a list of places that they access when searching for class files. Buried deep inside of your Java directory tree is a subdirectory tree named jre/lib/ext. If you put JAR files into the ext subdirectory, java and javac can find them, and use the class files they contain. You don't have to mention these subdirectories in a classpath statement—searching this directory is a function that's built right into Java. Sun recommends, however, that you use this feature only for your own internal testing and development, and not for software that you intend to distribute.

image from book
Exam Watch

It's possible to create environment variables that provide an alias for long classpaths.The classpath for some of the JAR files in J2SE can be quite long, and so it's common for such an alias to be used when defining a classpath. If you see something like JAVA_HOME or $JAVA_HOME in an exam question it just means "That part of the absolute classpath up to the directories we're specifying explicitly." You can assume that the JAVA_HOME literal means this, and is pre-pended to the partial classpath you see.

image from book




SCJP Sun Certified Programmer for Java 5 Study Guide Exam 310-055
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press)
ISBN: 0072253606
EAN: 2147483647
Year: 2006
Pages: 131

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