Monday, May 28, 2007

JAR

In computing, a JAR file (or Java ARchive) file used to distribute a set of Java classes. It is used to store compiled Java classes and linked metadata that can constitute a program.

* WAR (file format) (Web Application aRchive) files are also Java archives which store XML files, java classes, Java Server Pages and other objects for Web Applications.
* EAR (file format) (Enterprise ARchive) files are also Java archives which store XML files, java classes and other objects for Enterprise Applications.
* RAR (file format) (Resource Adapter aRchive) files are also Java archives which store XML files, java classes and other objects for J2EE Connector Architecture (JCA) applications.

JAR files can be created and extracted using the "jar" command that comes with the JDK. It can be done using zip tools, but as WinZip has a habit of renaming all-uppercase directories and files in lower case, this can raise support calls with whoever shaped the JAR or the tool authors themselves. WinRAR, on the other hand, retains the original case of filenames.

A JAR file has a manifest file located in the path META-INF/MANIFEST.MF. The entries in the manifest file determine how the JAR file will be used. JAR files which are intended to be executed as standalone programs will have one of their classes specified as the "main" class. The manifest file would have an entry such as
Main-Class: myPrograms.MyClass

Such JAR files are typically started with a command similar to

java -jar foo.jar

These files can also include a Classpath entry, which identifies other JAR files to be loaded with the JAR. This entry consists of a list of absolute or relative paths to other JAR files. Although intended to simplify JAR use, in practice, it turns out to be notoriously brittle as it depends on all the relevant JARs being in the exact locations specified when the entry-point JAR was built. To change versions or locations of libraries, a new manifest is needed.

No comments: