JAR Archives

JAR archives are the standard means of packaging and distributing Java software. They are used by applets, servlets, standalone GUI applications, class libraries, JavaBeans, and more. The content in a JAR archive can be found by the class loader, regardless of the file's exact location in the filesystem, as long as it is somewhere in the classpath. This makes JARs a very convenient place to put configuration data, preferences, lookup tables, localization strings, and other noncode resources that need to be distributed with an application. In particular, storing such resources in JAR archives enables you to read them using standard streams without:

JAR files also improve performance, especially in applications such as applets and Java Web Start-launched applications that download their code from a server. First of all, the content in the JAR archive is compressed. More importantly, it is faster for a web browser to download one JAR file than to download all the individual files the archive contains, since only one HTTP connection is required. Storing resources in a JAR file makes your applications faster, more robust, harder to accidentally break, and easier to install.

Sun wisely decided not to define a new archive format for JAR files. Instead, they stuck with the tried-and-true zip format. However, a JAR file also contains some extra metadata you won't find in a typical zip file. To a pure zip tool, this metadata just looks like some files and directories in the archive. However, to a JAR tool, that extra metadata provides key information your programs can use.

To make the files contained in the archive available to Java, the complete path to the archive itself is added to the classpath. The JAR file is treated like a directory in the context of the classpath. This is sensible, because although the archive is a file to the filesystem, it behaves like a directory to Java. Alternately, you can just put the JAR file in the jre/lib/ext directory or the jre/lib/endorsed directory, where all class loaders will find it automatically.

Категории