JAR (file format)

Last updated
Java Archive
Nuvola mimetypes java jar.png
KDE JAR file icon
Filename extension
.jar
Internet media type
application/java-archive [1] [2] [3]
Uniform Type Identifier (UTI) com.sun.java-archive
Magic number 50 4b 03 04 ASCII:PK\x03\x04
Developed by Netscape, Sun Microsystems, Oracle Corporation
Type of format File archive, data compression
Extended from ZIP

A JAR ("Java archive") file is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file for distribution. [4]

Contents

JAR files are archive files that include a Java-specific manifest file. They are built on the ZIP format and typically have a .jar file extension. [5]

Design

A JAR file allows Java runtimes to efficiently deploy an entire application, including its classes and their associated resources, in a single request. JAR file elements may be compressed, shortening download times.

A JAR file may contain a manifest file, that is located at META-INF/MANIFEST.MF. The entries in the manifest file describe how to use the JAR file. For instance, a Classpath entry can be used to specify other JAR files to load with the JAR.

Extraction

The contents of a file may be extracted using any archive extraction software that supports the ZIP format, or the jar command line utility provided by the Java Development Kit.

Security

Developers can digitally sign JAR files. In that case, the signature information becomes part of the embedded 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 gaining access to package-scoped classes and data.

The content of JAR files may be obfuscated to make reverse engineering more difficult.

Executable JAR files

An executable Java program can be packaged in a JAR file, along with any libraries the program uses. Executable JAR files have the manifest specifying the entry point class with Main-Class: myPrograms.MyClass and an explicit Class-Path (and the -cp argument is ignored). Some operating systems can run these directly when clicked. The typical invocation is java -jar foo.jar from a command line.

Native launchers can be created on most platforms. For instance, Microsoft Windows users who prefer having Windows EXE files can use tools such as JSmooth, Launch4J, WinRun4J or Nullsoft Scriptable Install System to wrap single JAR files into executables.

Manifest

A manifest file is a metadata file contained within a JAR. [6] [7] It defines extension and package-related data. It contains name–value pairs organized in sections. If a JAR file is intended to be used as an executable file, the manifest file specifies the main class of the application. The manifest file is named MANIFEST.MF. The manifest directory has to be the first entry of the compressed archive.

Specifications

The manifest appears at the canonical location META-INF/MANIFEST.MF. [8] There can be only one manifest file in an archive and it must be at that location.

The content of the manifest file in a JAR file created with version 1.0 of the Java Development Kit is the following.

Manifest-Version: 1.0

The name is separated from its value by a colon. The default manifest shows that it conforms to version 1.0 of the manifest specification.

The manifest can contain information about the other files that are packaged in the archive. Manifest contents depend on the intended use for the JAR file. The default manifest file makes no assumptions about what information it should record about other files, so its single line contains data only about itself. It should be encoded in UTF-8.

Special-Purpose Manifest Headers

JAR files created only for the purpose of archiving do not use the MANIFEST.MF file.

Most uses of JAR files go beyond simple archiving and compression and require special information in the manifest file.

Features

The manifest allows developers to define several useful features for their jars. Properties are specified in key-value pairs.

Applications

If an application is contained in a JAR file, the Java Virtual Machine needs to know the application's entry point. An entry point is any class with a public static void main(String[] args) method. This information is provided in the manifest Main-Class header, which has the general form:

Main-Class: com.example.MyClassName

In this example com.example.MyClassName.main() executes at application launch.

Package Sealing

Optionally, a package within a JAR file can be sealed, which means that all classes defined in that package are archived in the same JAR file. A package might be sealed to ensure version consistency among the classes in the software or as a security measure.

To seal a package, a Name entry needs to appear, followed by a Sealed header, such as:

Name:myCompany/myPackage/Sealed:true

The Name header's value is the package's relative pathname. Note that it ends with a '/' to distinguish it from a filename. Any headers following a Name header, without any intervening blank lines, apply to the file or package specified in the Name header. In the above example, because the Sealed header occurs after the Name: myCompany/myPackage header with no intervening blank lines, the Sealed header applies (only) to the package myCompany/myPackage.

The feature of sealed packages is outmoded by the Java Platform Module System introduced in Java 9, in which modules cannot split packages. [9]

Package Versioning

Several manifest headers hold versioning information. One set of headers can be assigned to each package. The versioning headers appear directly beneath the Name header for the package. This example shows all the versioning headers:

Name:java/util/Specification-Title:"Java Utility Classes"Specification-Version:"1.2"Specification-Vendor:"Sun Microsystems, Inc.".Implementation-Title:"java.util"Implementation-Version:"build57"Implementation-Vendor:"Sun Microsystems, Inc."

Multi-Release

A jar can be optionally marked as a multi-release jar. Using the multi-release feature allows library developers to load different code depending on the version of the Java runtime. [10] This in turn allows developers to leverage new features without sacrificing compatibility.

A multi-release jar is enabled using the following declaration in the manifest:

Multi-Release:true

Dependencies

The MANIFEST.MF file can be used to specify all the classes that must be loaded for an application to be able to run. [11]

Note that Class-Path entries are delimited with spaces, not with the system path delimiter:

Class-Path:. pkg1.jar path/to/pkg2.jar

Apache Ant Zip/JAR support

The Apache Ant build tool has its own package to read and write Zip and JAR archives, including support for 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.

Several related file formats build on the JAR format:

See also

Related Research Articles

<span class="mw-page-title-main">OSGi</span> Open standards organisation

OSGi is an open specification and open source project under the Eclipse Foundation. It is a continuation of the work done by the OSGi Alliance, which was an open standards organization for computer software founded in March 1999. The foundation originally specified and maintained the OSGi standard. The alliance transferred its work to the Eclipse Foundation at the end of 2020. The OSGi specification describes a modular system and a service platform for the Java programming language that implements a complete and dynamic component model, something that does not exist in standalone Java or VM environments. It has a service-oriented architecture based on micro services each implemented as an extended Java class file archive.

ZIP is an archive file format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed. The ZIP file format permits a number of compression algorithms, though DEFLATE is the most common. This format was originally created in 1989 and was first implemented in PKWARE, Inc.'s PKZIP utility, as a replacement for the previous ARC compression format by Thom Henderson. The ZIP format was then quickly supported by many software utilities other than PKZIP. Microsoft has included built-in ZIP support in versions of Microsoft Windows since 1998 via the "Plus! 98" addon for Windows 98. Native support was added as of the year 2000 in Windows ME. Apple has included built-in ZIP support in Mac OS X 10.3 and later. Most free operating systems have built in support for ZIP in similar manners to Windows and Mac OS X.

<span class="mw-page-title-main">Eclipse (software)</span> Software development environment

Eclipse is an integrated development environment (IDE) used in computer programming. It contains a base workspace and an extensible plug-in system for customizing the environment. It is the second-most-popular IDE for Java development, and, until 2016, was the most popular. Eclipse is written mostly in Java and its primary use is for developing Java applications, but it may also be used to develop applications in other programming languages via plug-ins, including Ada, ABAP, C, C++, C#, Clojure, COBOL, D, Erlang, Fortran, Groovy, Haskell, JavaScript, Julia, Lasso, Lua, NATURAL, Perl, PHP, Prolog, Python, R, Ruby, Rust, Scala, and Scheme. It can also be used to develop documents with LaTeX and packages for the software Mathematica. Development environments include the Eclipse Java development tools (JDT) for Java and Scala, Eclipse CDT for C/C++, and Eclipse PDT for PHP, among others.

<span class="mw-page-title-main">Apache Ant</span> Java software tool

Apache Ant is a software tool for automating software build processes which originated from the Apache Tomcat project in early 2000 as a replacement for the Make build tool of Unix. It is similar to Make, but is implemented using the Java language and requires the Java platform. Unlike Make, which uses the Makefile format, Ant uses XML to describe the code build process and its dependencies.

Maven is a build automation tool used primarily for Java projects. Maven can also be used to build and manage projects written in C#, Ruby, Scala, and other languages. The Maven project is hosted by the Apache Software Foundation, where it was formerly part of the Jakarta Project.

In computing, an archive file is a computer file that is composed of one or more files along with metadata. Archive files are used to collect multiple data files together into a single file for easier portability and storage, or simply to compress files to use less storage space. Archive files often store directory structures, error detection and correction information, arbitrary comments, and sometimes use built-in encryption.

EAR is a file format used by Jakarta 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 contains XML files called deployment descriptors which describe how to deploy the modules.

In software engineering, a WAR file is a file used to distribute a collection of JAR-files, JavaServer Pages, Java Servlets, Java classes, XML files, tag libraries, static web pages and other resources that together constitute a web application.

The Java Class Loader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. Usually classes are only loaded on demand. The Java run time system does not need to know about files and file systems as this is delegated to the class loader.

<span class="mw-page-title-main">Apache Felix</span> Open-source OSGi framework

Apache Felix is an open source implementation of the OSGi Core Release 6 framework specification. The initial codebase was donated from the Oscar project at ObjectWeb. The developers worked on Felix for a full year and have made various improvements while retaining the original footprint and performance. On June 21, 2007, the project graduated from incubation as a top level project and is considered the smallest size software at Apache Software Foundation.

The Java Platform Module System specifies a distribution format for collections of Java code and associated resources. It also specifies a repository for storing these collections, or modules, and identifies how they can be discovered, loaded and checked for integrity. It includes features such as namespaces with the aim of fixing some of the shortcomings in the existing JAR format, especially the JAR Hell, which can lead to issues such as classpath and class loading problems.

A manifest file in computing is a file containing metadata for a group of accompanying files that are part of a set or coherent unit. For example, the files of a computer program may have a manifest describing the name, version number, license and the constituent files of the program.

Action Message Format (AMF) is a binary format used to serialize object graphs such as ActionScript objects and XML, or send messages between an Adobe Flash client and a remote service, usually a Flash Media Server or third party alternatives. The Actionscript 3 language provides classes for encoding and decoding from the AMF format.

The Android Package with the file extension apk is the file format used by the Android operating system, and a number of other Android-based operating systems for distribution and installation of mobile apps, mobile games and middleware. A file using this format can be built from source code written in either Java or Kotlin.

Apache Click is a page and component oriented web application framework for the Java language and is built on top of the Java Servlet API.

The Java Development Kit (JDK) is a distribution of Java Technology by Oracle Corporation. It implements the Java Language Specification (JLS) and the Java Virtual Machine Specification (JVMS) and provides the Standard Edition (SE) of the Java Application Programming Interface (API). It is derivative of the community driven OpenJDK which Oracle stewards. It provides software for working with Java applications. Examples of included software are the virtual machine, a compiler, performance monitoring tools, a debugger, and other utilities that Oracle considers useful for a Java programmer.

Virgo is an open source, OSGi-based, Java application server. Virgo supports the deployment of OSGi bundles and unmodified Java web applications as well as OSGi-influenced Shared Libraries WARs and Shared Services WARs.

This article compares the application programming interfaces (APIs) and virtual machines (VMs) of the programming language Java and operating system Android.

References

  1. "File Extension .JAR Details" . Retrieved 29 November 2012.
  2. "MIME : Java Glossary" . Retrieved 29 November 2012.
  3. "IANA Assignment" . Retrieved 12 May 2023.
  4. "JDK 6 Java Archive (JAR)-related APIs & DeveloperGuides".
  5. "JAR File Specification". Java SE Documentation. Oracle. Archived from the original on 2017-09-11.
  6. "Understanding the Manifest". Java.sun.com. 2003-03-21. Retrieved 2012-07-31.
  7. "JAR File Specification". Download.oracle.com. Retrieved 2012-07-31.
  8. "JAR File Specification". Download.oracle.com. Retrieved 2012-07-31.
  9. "JEP 261: Module System" . Retrieved 2021-02-06.
  10. "JEP 238: Multi-Release JAR Files" . Retrieved 2021-02-06.
  11. the sun servlet specification, page 72 (servlet-2_4-fr-spec.pdf). See also the Java Tech Notes.
  12. "Glossary". developer.android.com.
  13. "Android library". developer.android.com.