JAR (file format)

JAR (file format)

Infobox file format
name = Java Archive
icon =
extension = .jar
mime = application/java-archive
type code =
uniform type = com.sun.java-archive
owner = Sun Microsystems
genre = file archive, data compression
container for =
contained by =
extended from = ZIP
extended to =
standard =
In computing, a JAR file (or Java ARchive) is used for aggregating many files into one. It is generally used to distribute Java classes and associated metadata.

Overview

JAR files are based on the ZIP file format. JAR files can be created and extracted using the "jar" command that comes with the JDK. It can also be done using zip tools, but care must be taken that the zip tool does not fold filenames to all-uppercase or all-lowercase.

A JAR file has an optional 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.

A JAR file can be digitally signed. If so, the signature information is added to the manifest file. The JAR itself is not signed, but instead every file inside the archive is listed along with its checksum; it is these checksums that are signed. Multiple entities may sign the JAR file, changing the JAR file itself with each signing, although the signed files themselves remain valid. When the Java runtime loads signed JAR files, it can validate the signatures and refuse to load classes that do not match the signature. It can also support 'sealed' packages, in which the Classloader will only permit Java classes to be loaded into the same package if they are all signed by the same entities. This prevents malicious code from being inserted into an existing package, and so gain access to package-scoped classes and data.

JAR files can be obfuscated so that a user of the JAR file doesn't get much information regarding the code it contains, or to reduce its size, which is useful in mobile phone applications.

For those Microsoft Windows users who prefer having Windows EXE files, tools such as JSmooth, Launch4J, WinRun4J or [http://nsis.sourceforge.net/Java_Launcher_with_automatic_JRE_installation NSIS - Java Launcher with automatic JRE installation] can be used to wrap JAR files into executables. Eclipse uses a small EXE launcher (eclipse.exe) to display the splash screen on startup and launch the application from the main JAR (startup.jar).

Apache Ant Zip/JAR support

The Apache Ant build tool has its own package to read and write the Zip and JAR archives, including support for the Unix filesystem extensions. The org.apache.tools.zip package is released under the Apache Software Foundation license and is designed to be usable outside Ant. This code is fast, widely used, and creates most JAR files that are not created with Sun's utility, so it could be considered fairly mature. Fact|date=March 2008

Problems with the JAR format

The Ant team found that most of their support calls related to JAR file creation have two underlying causes.Fact|date=June 2008

The first is manifest creation, specifically how long lines in the manifest are wrapped. This is a complex and somewhat ambiguous part of the specification. Ant wraps long lines at 68 characters and continues on the following line with a space at the front to indicate a continuation. This is viewed as erroneous by people that have not read the specification in detail and believe that the Classpath should be split at a file boundary, instead of partly across a file name. Unfortunately, if that is done, the Java runtime does not detect a split line as the first line ends before the 68 character boundary.

The second is WinZip converting upper-case files and directories to lower case. [cite web | title = Apache Ant - Frequently Asked Questions | url = http://ant.apache.org/faq.html#winzip-lies] If a user views the contents of a JAR file using an older versionwhich|date=June 2008 of WinZip, a file such as MANIFEST/MANIFEST.MF is converted to manifest/manifest.mf. This has been remedied in modernwhich|date=June 2008 versions of WinZip, which now respects file name case. Some mobile phone Java runtimes appear to parse the manifest in ways that are incompatible with the specification, and require a strict ordering of entries in the manifest. They also do not implement the line wrapping algorithm correctly. This may imply a problem in the test suite for the J2ME mobile Java runtime.

Related formats

There are several related file formats based on the JAR format:

*WAR (Web Application aRchive) files are also Java archives which store XML files, Java classes, JavaServer Pages and other objects for Web Applications.

*RAR (Resource Adapter aRchive) files, not to be confused with the RAR file format, are also Java archives which store XML files, Java classes and other objects for J2EE Connector Architecture (JCA) applications.

*EAR (Enterprise ARchive) files are composite Java archives which combine XML files, Java classes and other objects including JAR, WAR and RAR Java archive files for Enterprise Applications.

ee also

* WAR (file format)
* EAR (file format)
* Classpath
* JAR hell

References

External links

* [http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html JAR File Specification]
* [http://java.sun.com/developer/Books/javaprogramming/JAR/basics/ Using JAR files: The Basics (from Sun)]
* [http://java.sun.com/docs/books/tutorial/deployment/jar/ Lesson: Packaging Programs in JAR Files (from Sun)]
* [http://java.sun.com/j2se/1.5.0/docs/guide/jar/jarGuide.html Jar File Overview (from Sun)]
* [http://jcloader.sourceforge.net Jar Class Loader (dynamically loading classes directly from Jar files)]
* [http://savannah.nongnu.org/projects/fastjar fastjar] - alternative .jar creation program written in C


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • EAR (file format) — An Enterprise ARchive, or EAR, is a file format used by Java EE for packaging one or more modules into a single archive so that the deployment of the various modules onto an application server happens simultaneously and coherently. It also… …   Wikipedia

  • WAR (Sun file format) — Infobox file format name = Web ARchive extension = .war mime = owner = Sun Microsystems creatorcode = genre = containerfor = JSP, Java Servlet containedby = extendedfrom = extendedto = In computing, a WAR file (which stands for web application… …   Wikipedia

  • ZIP (file format) — unzip redirects here. For the program, see Info ZIP. ZIP Filename extension .zip .zipx (newer compression algorithms) Internet media type application/zip Uniform Type Identifier com.pkware.zip archive Magic …   Wikipedia

  • tar (file format) — tar GNU tar 1.23 showing three common types of Tarballs (shown in red). Filename extension .tar Internet media type application/x tar …   Wikipedia

  • JAD (file format) — Infobox file format name = Java application descriptor icon = logo = caption = extension = .jad mime = text/vnd.sun.j2me.app descriptor type code = uniform type = magic = owner = Sun Microsystems, Inc. genre = description container for =… …   Wikipedia

  • .jar — (format de fichier) Java Archive Extension de fichier .jar Type MIME application/java archive Développé par Sun Microsystems …   Wikipédia en Français

  • Jar — (format de fichier) Java Archive Extension de fichier .jar Type MIME application/java archive Développé par Sun Microsystems …   Wikipédia en Français

  • WAR (file format) (disambiguation) — In computing, a WAR file (short for Web ARchive) may refer to: * Sun WAR (file format), a JAR file used to distribute a collection of JavaServer Pages, servlets, Java classes, XML files, tag libraries and static Web pages (HTML and related files) …   Wikipedia

  • deb (file format) — Debian package The GNOME icon for deb files Filename extension .deb Internet media type application/x deb Developed by …   Wikipedia

  • JAR (format de fichier) — Pour les articles homonymes, voir JAR (homonymie). Java Archive Extension .jar …   Wikipédia en Français

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”