How to Read and Write Files in the Jar File in Java

Java archive file format

Java Archive
KDE JAR file icon
Filename extension

.jar

Internet media type

application/java-archive[1] [2]

Compatible Type Identifier (UTI) com.lord's day.java-archive
Magic number 50 4b 03 04 ASCII:PK\x03\x04
Developed by Netscape, Dominicus Microsystems, Oracle Corporation
Blazon of format File archive, information pinch
Extended from ZIP

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

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

Design [edit]

A JAR file allows Java runtimes to efficiently deploy an entire application, including its classes and their associated resource, in a unmarried asking. 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 draw how to use the JAR file. For instance, a Classpath entry can be used to specify other JAR files to load with the JAR.

[edit]

The contents of a file may be extracted using any standard compression software, or the jar command line utility: " -xf foo.".

Security [edit]

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 within the archive is listed along with its checksum; information technology 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 tin validate the signatures and refuse to load classes that do not match the signature. It can also back up 'sealed' packages, in which the Classloader will merely permit Coffee classes to be loaded into the same package if they are all signed by the aforementioned entities. This prevents malicious lawmaking from being inserted into an existing packet, and so gaining access to bundle-scoped classes and information.

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

Executable JAR files [edit]

An executable Coffee program can exist packaged in a JAR file, forth with any libraries the programme uses. Executable JAR files have the manifest specifying the entry bespeak class with Main-Course: 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 example, Microsoft Windows users who prefer having Windows EXE files tin use tools such as JSmooth, Launch4J, WinRun4J or Nullsoft Scriptable Install System to wrap single JAR files into executables.

Manifest [edit]

A manifest file is a metadata file contained within a JAR.[5] [6] 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 form of the application. The manifest file is named MANIFEST.MF. The manifest directory has to be the get-go entry of the compressed archive.

Specifications [edit]

The manifest appears at the canonical location META-INF/MANIFEST.MF.[7] There can be simply 1 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 i.0 of the Coffee Development Kit is the following.

Manifest-Version: 1.0        

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

The manifest tin contain information virtually 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 information technology should record nearly other files, so its unmarried line contains data but about itself. Information technology should be encoded in UTF-8.

[edit]

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

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

Features [edit]

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

Applications [edit]

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 form with a public static void main(String[] args) method. This information is provided in the manifest Master-Form header, which has the full general form:

Master-Class: com.example.MyClassName        

In this instance com.example.MyClassName.primary() executes at awarding launch.

Parcel Sealing [edit]

Optionally, a parcel within a JAR file can be sealed, which means that all classes divers in that package are archived in the aforementioned 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 parcel, a Name entry needs to appear, followed past a Sealed header, such as:

                        Name            :            myCompany/myPackage/            Sealed            :            truthful          

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 post-obit a Proper name header, without any intervening blank lines, apply to the file or packet specified in the Name header. In the above case, because the Sealed header occurs subsequently the Name: myCompany/myPackage header with no intervening blank lines, the Sealed header applies (simply) to the parcel myCompany/myPackage.

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

Package Versioning [edit]

Several manifest headers hold versioning information. One fix of headers can be assigned to each package. The versioning headers announced directly beneath the Proper noun header for the package. This instance shows all the versioning headers:

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

Multi-Release [edit]

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

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

Dependencies [edit]

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

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 [edit]

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.zilch package is released under the Apache Software Foundation license and is designed to be usable outside Emmet.

[edit]

Several related file formats build on the JAR format:

  • War (Web application annal) files, also Java archives, shop XML files, Java classes, JavaServer Pages and other objects for Web Applications.
  • RAR (resource adapter archive) files (not to exist dislocated with the RAR file format), as well Java archives, store XML files, Java classes and other objects for J2EE Connector Architecture (JCA) applications.
  • EAR (enterprise archive) files provide composite Java archives that combine XML files, Java classes and other objects including JAR, WAR and RAR Java annal files for Enterprise Applications.
  • SAR (service archive) is similar to EAR. It provides a service.xml file and accompanying JAR files.
  • APK (Android application package), a variant of the Java annal format, is used for Android applications.[xi]
  • AAR (Android archive) is used for distribution of Android libraries, typically via Maven.[12]
  • PAR (plan archive) - supported by Eclipse Virgo OSGi application server, allows the deployment of multi-bundle OSGi applications equally a single archive and provides isolation from other PAR-based applications deployed in the same server.
  • KAR (Karaf archive) - supported by Apache Karaf OSGi application server, allows the deployment of multi-bundle, multi-feature OSGi applications.

Run across too [edit]

  • JAR hell
  • Java Module Organization
  • Open Packaging Conventions
  • JAD (file format)
  • Gifar

References [edit]

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

External links [edit]

  • JAR File Overview
  • JAR File Specification

How to Read and Write Files in the Jar File in Java

Source: https://en.wikipedia.org/wiki/JAR_(file_format)

0 Response to "How to Read and Write Files in the Jar File in Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel