Archiving Classes with Jar


jar cf project.jar *.class



The jar utility is included with the JDK, and is used to package groups of classes into a bundle. Using the jar tool, you can create, update, extract, list, and index a JAR file. In this phrase, all the classes contained in the current directory from which the jar command is run will be put into a JAR file with the name project.jar. The c option tells the jar utility to create a new archive file. The f option is always followed by a filename specifying the name of the JAR file to use.

Complete applications can be distributed as JAR files. Applications can also be executed out of a JAR file without having to first extract them. See the phrase "Running a Program from a JAR File" contained in this chapter for more information about doing this.

All classes contained in a JAR file can be easily included on the CLASSPATH when running or compiling a Java application or library. To include the contents of a JAR file in the CLASSPATH, you simply include the path to the JAR file instead of a directory. For example, your CLASSPATH statement might be similar to the following:

CLASSPATH=.;c:\projects\fisher.jar;c:\projects\classes


This would include all the classes contained in the archive fisher.jar in your CLASSPATH. It is important to note that to include the classes in a JAR file, you must specify the name of the JAR file in the classpath. You cannot just point to a directory containing multiple JAR files as you can for .class files.

For more detailed information about using the jar tool, refer to the official JAR documentation from Sun available at: http://java.sun.com/j2se/1.5.0/docs/guide/jar/index.html.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

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