APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.util.jar

public Class JarFile

extends ZipFile
Class Inheritance
Known Direct Subclasses
sun.net.www.protocol.jar.URLJarFile
Imports
jdk.internal.access.SharedSecrets, .JavaUtilZipFileAccess, sun.security.util.ManifestEntryVerifier, .SignatureFileVerifier, java.io.ByteArrayInputStream, .EOFException, .File, .IOException, .InputStream, java.lang.ref.SoftReference, java.security.CodeSigner, java.security.cert.Certificate, java.util.BitSet, .Enumeration, .List, .Objects, java.util.stream.Stream, java.util.zip.ZipEntry, .ZipException, .ZipFile

The JarFile class is used to read the contents of a jar file from any file that can be opened with java.io.RandomAccessFile. It extends the class java.util.zip.ZipFile with support for reading an optional Manifest entry, and support for processing multi-release jar files. The Manifest can be used to specify meta-information about the jar file and its entries.

A multi-release jar file is a jar file that contains a manifest with a main attribute named "Multi-Release", a set of "base" entries, some of which are public classes with public or protected methods that comprise the public interface of the jar file, and a set of "versioned" entries contained in subdirectories of the "META-INF/versions" directory. The versioned entries are partitioned by the major version of the Java platform. A versioned entry, with a version n, 8 < n, in the "META-INF/versions/{n}" directory overrides the base entry as well as any entry with a version number i where 8 < i < n.

By default, a JarFile for a multi-release jar file is configured to process the multi-release jar file as if it were a plain (unversioned) jar file, and as such an entry name is associated with at most one base entry. The JarFile may be configured to process a multi-release jar file by creating the JarFile with the JarFile#JarFile(File, boolean, int, Runtime.Version) constructor. The Runtime.Version object sets a maximum version used when searching for versioned entries. When so configured, an entry name can correspond with at most one base entry and zero or more versioned entries. A search is required to associate the entry name with the latest versioned entry whose version is less than or equal to the maximum version (see getEntry(String)).

Class loaders that utilize JarFile to load classes from the contents of JarFile entries should construct the JarFile by invoking the JarFile#JarFile(File, boolean, int, Runtime.Version) constructor with the value Runtime.version() assigned to the last argument. This assures that classes compatible with the major version of the running JVM are loaded from multi-release jar files.

If the verify flag is on when opening a signed jar file, the content of the jar entry is verified against the signature embedded inside the manifest that is associated with its path name. For a multi-release jar file, the content of a versioned entry is verified against its own signature and JarEntry#getCodeSigners() returns its own signers.

Please note that the verification process does not include validating the signer's certificate. A caller should inspect the return value of JarEntry#getCodeSigners() to further determine if the signature can be trusted.

Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

Implementation Note

If the API can not be used to configure a JarFile (e.g. to override the configuration of a compiled application or library), two System properties are available.
Author
David Connelly
Since
1.2
See Also
Manifest, java.util.zip.ZipFile, java.util.jar.JarEntry

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private static final Runtime.Version
private static final int
private static final byte[]
private static final byte[]
private static final byte[]
private volatile boolean
private boolean
private static final ScopedValue<Boolean>
pack-priv static final String
INDEX_NAME

The 'JAR index' feature has been removed, but JarInputStream and JarVerifier's verification of signed jars still need to be able to skip this entry.

private boolean
private static final JavaUtilZipFileAccess
private JarVerifier
private boolean
private JarEntry
public static final String
MANIFEST_NAME

The JAR manifest file name.

private SoftReference<Manifest>
private static final String
private static final String
private static final boolean
private static final boolean
private static final byte[]
private static final byte[]
private static final byte[]
private static final Runtime.Version
private boolean
private final Runtime.Version
private final int
Inherited from java.util.zip.ZipFile:
OPEN_DELETEOPEN_READ

Constructor Summary

AccessConstructor and Description
public
JarFile(String
the name of the jar file to be opened for reading
name
)

Creates a new JarFile to read from the specified file name.

public
JarFile(String
the name of the jar file to be opened for reading
name
,
boolean
whether or not to verify the jar file if it is signed.
verify
)

Creates a new JarFile to read from the specified file name.

public
JarFile(File
the jar file to be opened for reading
file
)

Creates a new JarFile to read from the specified File object.

public
JarFile(File
the jar file to be opened for reading
file
,
boolean
whether or not to verify the jar file if it is signed.
verify
)

Creates a new JarFile to read from the specified File object.

public
JarFile(File
the jar file to be opened for reading
file
,
boolean
whether or not to verify the jar file if it is signed.
verify
,
int
the mode in which the file is to be opened
mode
)

Creates a new JarFile to read from the specified File object in the specified mode.

public
JarFile(File
the jar file to be opened for reading
file
,
boolean
whether or not to verify the jar file if it is signed.
verify
,
int
the mode in which the file is to be opened
mode
,
Runtime.Version
specifies the release version for a multi-release jar file
version
)

Creates a new JarFile to read from the specified File object in the specified mode.

Method Summary

Modifier and TypeMethod and Description
public static Runtime.Version

Returns:

the version that represents the unversioned configuration
baseVersion
()

Returns the version that represents the unversioned configuration of a multi-release jar file.

private void
checkForSpecialAttributes()

On first invocation, check if the JAR file has the Class-Path and the Multi-Release attribute.

pack-priv synchronized void
public Enumeration<JarEntry>

Returns:

an enumeration of the jar file entries
entries
()

Overrides java.util.zip.ZipFile.entries.

Returns an enumeration of the jar file entries.

pack-priv JarEntry
entryFor(String name)

Creates a ZipEntry suitable for the given ZipFile.

private String
private byte[]
public ZipEntry

Returns:

the ZipEntry for the given entry name or the versioned entry name or null if not found
getEntry
(String
the jar file entry name
name
)

Overrides java.util.zip.ZipFile.getEntry.

Returns the ZipEntry for the given base entry name or null if not found.

public synchronized InputStream

Returns:

an input stream for reading the contents of the specified ZIP file entry or null if the ZIP file entry does not exist within the jar file
getInputStream
(ZipEntry
the ZIP file entry
ze
)

Overrides java.util.zip.ZipFile.getInputStream.

Returns an input stream for reading the contents of the specified ZIP file entry.

public JarEntry

Returns:

the JarEntry for the given entry name, or the versioned entry name, or null if not found
getJarEntry
(String
the jar file entry name
name
)

Returns the JarEntry for the given base entry name or null if not found.

private JarEntry
public Manifest

Returns:

the jar file manifest, or null if none
getManifest
()

Returns the jar file manifest, or null if none.

private Manifest
pack-priv String
public final Runtime.Version

Returns:

the maximum version
getVersion
()

Returns the maximum version used when searching for versioned entries.

private JarEntry
getVersionedEntry(String name, JarEntry defaultEntry)

pack-priv boolean
hasClassPathAttribute()

Returns true iff this JAR file has a manifest with the Class-Path attribute

private void
pack-priv static boolean

Returns:

true if the current thread is initializing a JAR verifier, false otherwise
isInitializing
()

Returns true if the current thread is initializing a JAR verifier, false otherwise.

public final boolean

Returns:

true if this JarFile is a multi-release jar file
isMultiRelease
()

Indicates whether or not this jar file is a multi-release jar file.

private int
match(byte[] src, byte[] b, byte[] lastOcc, byte[] optoSft)

Returns true if the pattern src is found in b.

private void
public static Runtime.Version

Returns:

the version that represents the runtime versioned configuration
runtimeVersion
()

Returns the version that represents the effective runtime versioned configuration of a multi-release jar file.

public Stream<JarEntry>

Returns:

an ordered Stream of entries in this jar file
stream
()

Overrides java.util.zip.ZipFile.stream.

Returns an ordered Stream over the jar file entries.

private JarEntry
public Stream<JarEntry>

Returns:

stream of versioned entries
versionedStream
()

Returns a Stream of the versioned jar file entries.

Inherited from java.util.zip.ZipFile:
closegetCommentgetNamesizetoString

Field Detail

BASE_VERSIONback to summary
private static final Runtime.Version BASE_VERSION
BASE_VERSION_FEATUREback to summary
private static final int BASE_VERSION_FEATURE
CLASSPATH_CHARSback to summary
private static final byte[] CLASSPATH_CHARS
CLASSPATH_LASTOCCback to summary
private static final byte[] CLASSPATH_LASTOCC
CLASSPATH_OPTOSFTback to summary
private static final byte[] CLASSPATH_OPTOSFT
hasCheckedSpecialAttributesback to summary
private volatile boolean hasCheckedSpecialAttributes
hasClassPathAttributeback to summary
private boolean hasClassPathAttribute
IN_VERIFIER_INITback to summary
private static final ScopedValue<Boolean> IN_VERIFIER_INIT
INDEX_NAMEback to summary
pack-priv static final String INDEX_NAME

The 'JAR index' feature has been removed, but JarInputStream and JarVerifier's verification of signed jars still need to be able to skip this entry.

isMultiReleaseback to summary
private boolean isMultiRelease
JUZFAback to summary
private static final JavaUtilZipFileAccess JUZFA
jvback to summary
private JarVerifier jv
jvInitializedback to summary
private boolean jvInitialized
manEntryback to summary
private JarEntry manEntry
MANIFEST_NAMEback to summary
public static final String MANIFEST_NAME

The JAR manifest file name.

manRefback to summary
private SoftReference<Manifest> manRef
META_INFback to summary
private static final String META_INF
META_INF_VERSIONSback to summary
private static final String META_INF_VERSIONS
MULTI_RELEASE_ENABLEDback to summary
private static final boolean MULTI_RELEASE_ENABLED
MULTI_RELEASE_FORCEDback to summary
private static final boolean MULTI_RELEASE_FORCED
MULTIRELEASE_CHARSback to summary
private static final byte[] MULTIRELEASE_CHARS
MULTIRELEASE_LASTOCCback to summary
private static final byte[] MULTIRELEASE_LASTOCC
MULTIRELEASE_OPTOSFTback to summary
private static final byte[] MULTIRELEASE_OPTOSFT
RUNTIME_VERSIONback to summary
private static final Runtime.Version RUNTIME_VERSION
verifyback to summary
private boolean verify
versionback to summary
private final Runtime.Version version
versionFeatureback to summary
private final int versionFeature

Constructor Detail

JarFileback to summary
public JarFile(String name) throws IOException

Creates a new JarFile to read from the specified file name. The JarFile will be verified if it is signed.

Parameters
name:String

the name of the jar file to be opened for reading

Exceptions
IOException:
if an I/O error has occurred
JarFileback to summary
public JarFile(String name, boolean verify) throws IOException

Creates a new JarFile to read from the specified file name.

Parameters
name:String

the name of the jar file to be opened for reading

verify:boolean

whether or not to verify the jar file if it is signed.

Exceptions
IOException:
if an I/O error has occurred
JarFileback to summary
public JarFile(File file) throws IOException

Creates a new JarFile to read from the specified File object. The JarFile will be verified if it is signed.

Parameters
file:File

the jar file to be opened for reading

Exceptions
IOException:
if an I/O error has occurred
JarFileback to summary
public JarFile(File file, boolean verify) throws IOException

Creates a new JarFile to read from the specified File object.

Parameters
file:File

the jar file to be opened for reading

verify:boolean

whether or not to verify the jar file if it is signed.

Exceptions
IOException:
if an I/O error has occurred
JarFileback to summary
public JarFile(File file, boolean verify, int mode) throws IOException

Creates a new JarFile to read from the specified File object in the specified mode. The mode argument must be either OPEN_READ or OPEN_READ | OPEN_DELETE.

Parameters
file:File

the jar file to be opened for reading

verify:boolean

whether or not to verify the jar file if it is signed.

mode:int

the mode in which the file is to be opened

Exceptions
IOException:
if an I/O error has occurred
IllegalArgumentException:
if the mode argument is invalid
Since
1.3
JarFileback to summary
public JarFile(File file, boolean verify, int mode, Runtime.Version version) throws IOException

Creates a new JarFile to read from the specified File object in the specified mode. The mode argument must be either OPEN_READ or OPEN_READ | OPEN_DELETE. The version argument, after being converted to a canonical form, is used to configure the JarFile for processing multi-release jar files.

The canonical form derived from the version parameter is Runtime.Version.parse(Integer.toString(n)) where n is Math.max(version.feature(), JarFile.baseVersion().feature()).

Parameters
file:File

the jar file to be opened for reading

verify:boolean

whether or not to verify the jar file if it is signed.

mode:int

the mode in which the file is to be opened

version:Runtime.Version

specifies the release version for a multi-release jar file

Exceptions
IOException:
if an I/O error has occurred
IllegalArgumentException:
if the mode argument is invalid
NullPointerException:
if version is null
Since
9

Method Detail

baseVersionback to summary
public static Runtime.Version baseVersion()

Returns the version that represents the unversioned configuration of a multi-release jar file.

Returns:Runtime.Version

the version that represents the unversioned configuration

Since
9
checkForSpecialAttributesback to summary
private void checkForSpecialAttributes() throws IOException

On first invocation, check if the JAR file has the Class-Path and the Multi-Release attribute. A no-op on subsequent calls.

ensureInitializationback to summary
pack-priv synchronized void ensureInitialization()
entriesback to summary
public Enumeration<JarEntry> entries()

Overrides java.util.zip.ZipFile.entries.

Returns an enumeration of the jar file entries.

Returns:Enumeration<JarEntry>

an enumeration of the jar file entries

Exceptions
IllegalStateException:
may be thrown if the jar file has been closed
entryForback to summary
pack-priv JarEntry entryFor(String name)

Creates a ZipEntry suitable for the given ZipFile.

getBasenameback to summary
private String getBasename(String name)
getBytesback to summary
private byte[] getBytes(ZipEntry ze) throws IOException
getEntryback to summary
public ZipEntry getEntry(String name)

Overrides java.util.zip.ZipFile.getEntry.

Returns the ZipEntry for the given base entry name or null if not found.

If this JarFile is a multi-release jar file and is configured to be processed as such, then a search is performed to find and return a ZipEntry that is the latest versioned entry associated with the given entry name. The returned ZipEntry is the versioned entry corresponding to the given base entry name prefixed with the string "META-INF/versions/{n}/", for the largest value of n for which an entry exists. If such a versioned entry does not exist, then the ZipEntry for the base entry is returned, otherwise null is returned if no entries are found. The initial value for the version n is the maximum version as returned by the method JarFile#getVersion().

Implementation Specification

This implementation may return a versioned entry for the requested name even if there is not a corresponding base entry. This can occur if there is a private or package-private versioned entry that matches. If a subclass overrides this method, assure that the override method invokes super.getEntry(name) to obtain all versioned entries.
Parameters
name:String

the jar file entry name

Returns:ZipEntry

the ZipEntry for the given entry name or the versioned entry name or null if not found

Exceptions
IllegalStateException:
may be thrown if the jar file has been closed
See Also
java.util.zip.ZipEntry
getInputStreamback to summary
public synchronized InputStream getInputStream(ZipEntry ze) throws IOException

Overrides java.util.zip.ZipFile.getInputStream.

Returns an input stream for reading the contents of the specified ZIP file entry.

API Note

The InputStream returned by this method can wrap an InflaterInputStream, whose read(byte[], int, int) method can modify any element of the output buffer.

Parameters
ze:ZipEntry

the ZIP file entry

Returns:InputStream

an input stream for reading the contents of the specified ZIP file entry or null if the ZIP file entry does not exist within the jar file

Exceptions
IOException:
if an I/O error has occurred
ZipException:
if a ZIP file format error has occurred
SecurityException:
if any of the jar file entries are incorrectly signed.
IllegalStateException:
may be thrown if the jar file has been closed
getJarEntryback to summary
public JarEntry getJarEntry(String name)

Returns the JarEntry for the given base entry name or null if not found.

If this JarFile is a multi-release jar file and is configured to be processed as such, then a search is performed to find and return a JarEntry that is the latest versioned entry associated with the given entry name. The returned JarEntry is the versioned entry corresponding to the given base entry name prefixed with the string "META-INF/versions/{n}/", for the largest value of n for which an entry exists. If such a versioned entry does not exist, then the JarEntry for the base entry is returned, otherwise null is returned if no entries are found. The initial value for the version n is the maximum version as returned by the method JarFile#getVersion().

Implementation Specification

This implementation invokes JarFile#getEntry(String).
Parameters
name:String

the jar file entry name

Returns:JarEntry

the JarEntry for the given entry name, or the versioned entry name, or null if not found

Exceptions
IllegalStateException:
may be thrown if the jar file has been closed
See Also
java.util.jar.JarEntry
getManEntryback to summary
private JarEntry getManEntry()
getManifestback to summary
public Manifest getManifest() throws IOException

Returns the jar file manifest, or null if none.

Returns:Manifest

the jar file manifest, or null if none

Exceptions
IOException:
if an I/O error has occurred
IllegalStateException:
may be thrown if the jar file has been closed
getManifestFromReferenceback to summary
private Manifest getManifestFromReference() throws IOException
getRealNameback to summary
pack-priv String getRealName(JarEntry entry)
getVersionback to summary
public final Runtime.Version getVersion()

Returns the maximum version used when searching for versioned entries.

If this JarFile is not a multi-release jar file or is not configured to be processed as such, then the version returned will be the same as that returned from baseVersion().

Returns:Runtime.Version

the maximum version

Since
9
getVersionedEntryback to summary
private JarEntry getVersionedEntry(String name, JarEntry defaultEntry)
hasClassPathAttributeback to summary
pack-priv boolean hasClassPathAttribute() throws IOException

Returns true iff this JAR file has a manifest with the Class-Path attribute

initializeVerifierback to summary
private void initializeVerifier()
isInitializingback to summary
pack-priv static boolean isInitializing()

Returns true if the current thread is initializing a JAR verifier, false otherwise.

Returns:boolean

true if the current thread is initializing a JAR verifier, false otherwise

isMultiReleaseback to summary
public final boolean isMultiRelease()

Indicates whether or not this jar file is a multi-release jar file.

Returns:boolean

true if this JarFile is a multi-release jar file

Since
9
matchback to summary
private int match(byte[] src, byte[] b, byte[] lastOcc, byte[] optoSft)

Returns true if the pattern src is found in b. The lastOcc array is the precomputed bad character shifts. Since there are no repeated substring in our search strings, the good suffix shifts can be replaced with a comparison.

maybeInstantiateVerifierback to summary
private void maybeInstantiateVerifier() throws IOException
runtimeVersionback to summary
public static Runtime.Version runtimeVersion()

Returns the version that represents the effective runtime versioned configuration of a multi-release jar file.

By default the feature version number of the returned Version will be equal to the feature version number of Runtime.version(). However, if the jdk.util.jar.version property is set, the returned Version is derived from that property and feature version numbers may not be equal.

Returns:Runtime.Version

the version that represents the runtime versioned configuration

Since
9
streamback to summary
public Stream<JarEntry> stream()

Overrides java.util.zip.ZipFile.stream.

Returns an ordered Stream over the jar file entries. Entries appear in the Stream in the order they appear in the central directory of the jar file.

Returns:Stream<JarEntry>

an ordered Stream of entries in this jar file

Exceptions
IllegalStateException:
if the jar file has been closed
Since
1.8
verifiableEntryback to summary
private JarEntry verifiableEntry(ZipEntry ze) throws ZipException
versionedStreamback to summary
public Stream<JarEntry> versionedStream()

Returns a Stream of the versioned jar file entries.

If this JarFile is a multi-release jar file and is configured to be processed as such, then an entry in the stream is the latest versioned entry associated with the corresponding base entry name. The maximum version of the latest versioned entry is the version returned by getVersion(). The returned stream may include an entry that only exists as a versioned entry. If the jar file is not a multi-release jar file or the JarFile is not configured for processing a multi-release jar file, this method returns the same stream that stream() returns.

Returns:Stream<JarEntry>

stream of versioned entries

Since
10
java.util.jar back to summary

private Class JarFile.JarFileEntry

extends JarEntry
Class Inheritance

Field Summary

Modifier and TypeField and Description
private String
Inherited from java.util.jar.JarEntry:
attrcertssigners

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Attributes
getAttributes()

Overrides java.util.jar.JarEntry.getAttributes.

Returns the Manifest Attributes for this entry, or null if none.

public Certificate[]
getCertificates()

Overrides java.util.jar.JarEntry.getCertificates.

Returns the Certificate objects for this entry, or null if none.

public CodeSigner[]
getCodeSigners()

Overrides java.util.jar.JarEntry.getCodeSigners.

Returns the CodeSigner objects for this entry, or null if none.

public String
getName()

Overrides java.util.zip.ZipEntry.getName.

Returns the name of the entry.

public String
getRealName()

Overrides java.util.jar.JarEntry.getRealName.

Returns the real name of this JarEntry.

pack-priv JarFile.JarFileEntry
pack-priv JarFile.JarFileEntry

Field Detail

basenameback to summary
private String basename

Constructor Detail

JarFileEntryback to summary
pack-priv JarFileEntry(String name)
JarFileEntryback to summary
pack-priv JarFileEntry(String name, ZipEntry vze)

Method Detail

getAttributesback to summary
public Attributes getAttributes() throws IOException

Overrides java.util.jar.JarEntry.getAttributes.

Doc from java.util.jar.JarEntry.getAttributes.

Returns the Manifest Attributes for this entry, or null if none.

Annotations
@Override
Exceptions
IOException:
if an I/O error has occurred
getCertificatesback to summary
public Certificate[] getCertificates()

Overrides java.util.jar.JarEntry.getCertificates.

Doc from java.util.jar.JarEntry.getCertificates.

Returns the Certificate objects for this entry, or null if none. This method can only be called once the JarEntry has been completely verified by reading from the entry input stream until the end of the stream has been reached. Otherwise, this method will return null.

It is recommended to use the getCodeSigners method instead, which returns an array of CodeSigners.

The returned certificate array comprises all the signer certificates that were used to verify this entry. Each signer certificate is followed by its supporting certificate chain (which may be empty). Each signer certificate and its supporting certificate chain are ordered bottom-to-top (i.e., with the signer certificate first and the (root) certificate authority last).

Annotations
@Override
getCodeSignersback to summary
public CodeSigner[] getCodeSigners()

Overrides java.util.jar.JarEntry.getCodeSigners.

Doc from java.util.jar.JarEntry.getCodeSigners.

Returns the CodeSigner objects for this entry, or null if none. This method can only be called once the JarEntry has been completely verified by reading from the entry input stream until the end of the stream has been reached. Otherwise, this method will return null.

The returned array comprises all the code signers that have signed this entry.

Annotations
@Override
getNameback to summary
public String getName()

Overrides java.util.zip.ZipEntry.getName.

Doc from java.util.zip.ZipEntry.getName.

Returns the name of the entry.

Annotations
@Override
getRealNameback to summary
public String getRealName()

Overrides java.util.jar.JarEntry.getRealName.

Doc from java.util.jar.JarEntry.getRealName.

Returns the real name of this JarEntry. If this JarEntry is an entry of a multi-release jar file and the JarFile is configured to be processed as such, the name returned by this method is the path name of the versioned entry that the JarEntry represents, rather than the path name of the base entry that getName() returns. If the JarEntry does not represent a versioned entry of a multi-release JarFile or the JarFile is not configured for processing a multi-release jar file, this method returns the same name that getName() returns.

Annotations
@Override
realEntryback to summary
pack-priv JarFile.JarFileEntry realEntry()
withBasenameback to summary
pack-priv JarFile.JarFileEntry withBasename(String name)