A Java Archive (colloquially known as a JAR file due to the file extension ) is a ZIP file that can contain additional metadata in a file "
META-INF/MANIFEST.MF". JARs are used especially for distribution of Java - class libraries used and programs. The designation can be understood as a play on words with the English word jar (German: "vessel").
JAR files were originally introduced so that Java classes required by Java applets do not have to be reloaded individually from the network. Transferring many classes in one file is more efficient, and in addition, the files can be compressed .
The "manifest" file can be used to determine how the Java application is started. This means that the application can also be started under graphical user interfaces such as Windows , Mac OS X or KDE without the aid of the command line (provided
.jarthe appropriate command has been assigned to the file extension ). With
java -jaryou can start JAR files from the command line. JAR archives store file names internally in UTF-8 encoding so that they can also contain umlauts. An installed Java Runtime Environment is always required to run JARs or Java programs .
JAR files can be created with the jar command of the JDK (which uses the syntax of tar ) or, if the file names contain only ASCII characters, with any ZIP program. In addition, the Java Platform, Standard Edition offers in the two packages "java.util.jar" and "java.util.zip" classes to read or create JAR or ZIP archives.
For example, the following command displays the contents of a JAR file named test.jar .
jar tvf test.jar
In this case, the letter t for "Contents View" (of English t able of contents ), v for verbose output (of English v erbose ) and f states that (english from a file f ile ) is to be read whose name follows.
Each Java Archive can provide various information about the content of the archive in the "META-INF" directory through a file called "MANIFEST.MF". The most important meta-information includes
- the version of the included class libraries that can be determined at runtime ,
- Information about included JavaBeans and
- the name of the main class of a contained Java application .
This manifest file is a simple line-oriented text file that contains several pairs of names and values, each of which defines a so-called attribute . An attribute is a property of the entire application, the class library contained or even just a single Java package ( package ) or a single class . In addition, it is divided into several sections ( sections divided).
The first section is called main section and defines attributes related to the entire Java Archive. It always begins with the definition of the “Manifest Version” attribute, while the other attributes are optional. The following sections each refer to a single package or class and are optional, as are the attributes they contain. Unknown attributes are ignored and do not lead to error messages. If an attribute is defined both in the main section and in an individual section, the value defined in the individual section overlays the value pre-assigned in the main section for the component (package or class) to which the section relates.
The following example shows an excerpt from the manifest of the “rt.jar” file contained in the Java 1.4 runtime environment.
Manifest-Version: 1.0 Specification-Title: Java Platform API Specification Created-By: 1.4.2_05 (Sun Microsystems Inc.) Implementation-Title: Java Runtime Environment Specification-Vendor: Sun Microsystems, Inc. Specification-Version: 1.4 Implementation-Version: 1.4.2_05 Implementation-Vendor: Sun Microsystems, Inc. Name: javax/swing/JRadioButtonMenuItem.class Java-Bean: True Name: javax/swing/JList.class Java-Bean: True
The main section in this example shows that this manifest is structured as described in Version 1 of the Sun Microsystems JAR file specification (the only one so far). The other attributes of this main section provide information about the specification fulfilled by the library , the producer of the Java archive, the name of the implementation, as well as the manufacturer and version of the specification used and the implementation contained. The two following sections of the example each refer to a class that is marked as a JavaBean .
The Java Development Kit contains several programs for manipulating JAR files:
- jar is a program for creating, modifying and unpacking JAR files, the call parameters of which are similar to those of the well-known Unix program tar .
- jarsigner is a program that signs JAR files and verifies their electronic signature.
- pack200 converts JAR files into a file format that can store bytecode more efficiently. It was introduced in Java 5 and is used in particular with Java Web Start , since it may require large amounts of files to be transferred over the Internet . The reconversion takes place with the program unpack200 .
Programming tools for JAR files not included in the JDK:
- ProGuard is a program for compressing, optimizing and obfuscating JAR files. This is achieved through a more detailed analysis of the bytecode .
- If the file names in the archive consist of ASCII characters, JAR files can be edited with any software tool that can also edit ZIP files. Some examples are given in the list of data compression programs.