AI-generated Key Takeaways
-
The
JarFileclass is used to read the contents of a JAR file, extendingZipFileand supporting an optionalManifestentry for metadata. -
Null arguments passed to
JarFileconstructors or methods result in aNullPointerException. -
Signed JAR files can be verified upon opening, but the signer's certificate is not validated; use
JarEntry.getCodeSigners()for further inspection. -
JarFileprovides methods for retrieving specific entries, the manifest, an input stream for entry content, and enumerating all entries. -
JarFileinherits methods fromZipFileandObjectfor closing the file, getting entries, comments, and other functionalities.
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. The
Manifest can be used to specify meta-information about the
jar file and its entries.
Unless otherwise noted, passing a null argument to a constructor
or method in this class will cause a NullPointerException to be
thrown.
If the verify flag is on when opening a signed jar file, the content of the
file is verified against its signature embedded inside the file. 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.
Constant Summary
| String | MANIFEST_NAME | The JAR manifest file name. |
Inherited Constant Summary
Public Constructor Summary
Public Method Summary
| Enumeration<JarEntry> |
entries()
Returns an enumeration of the zip file entries.
|
| ZipEntry | |
| synchronized InputStream |
getInputStream(ZipEntry ze)
Returns an input stream for reading the contents of the specified
zip file entry.
|
| JarEntry | |
| Manifest |
getManifest()
Returns the jar file manifest, or
null if none. |
| Stream<JarEntry> |
stream()
Return an ordered
Stream over the ZIP file entries. |
Inherited Method Summary
Constants
public static final String MANIFEST_NAME
The JAR manifest file name.
Public Constructors
public JarFile (String name)
Creates a new JarFile to read from the specified
file name. The JarFile will be verified if
it is signed.
Parameters
| name | the name of the jar file to be opened for reading |
|---|
Throws
| IOException | if an I/O error has occurred |
|---|---|
| SecurityException | if access to the file is denied by the SecurityManager |
public JarFile (String name, boolean verify)
Creates a new JarFile to read from the specified
file name.
Parameters
| name | the name of the jar file to be opened for reading |
|---|---|
| verify | whether or not to verify the jar file if it is signed. |
Throws
| IOException | if an I/O error has occurred |
|---|---|
| SecurityException | if access to the file is denied by the SecurityManager |
public JarFile (File file)
Creates a new JarFile to read from the specified
File object. The JarFile will be verified if
it is signed.
Parameters
| file | the jar file to be opened for reading |
|---|
Throws
| IOException | if an I/O error has occurred |
|---|---|
| SecurityException | if access to the file is denied by the SecurityManager |
public JarFile (File file, boolean verify)
Creates a new JarFile to read from the specified
File object.
Parameters
| file | the jar file to be opened for reading |
|---|---|
| verify | whether or not to verify the jar file if it is signed. |
Throws
| IOException | if an I/O error has occurred |
|---|---|
| SecurityException | if access to the file is denied by the SecurityManager. |
public JarFile (File file, boolean verify, int mode)
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 | the jar file to be opened for reading |
|---|---|
| verify | whether or not to verify the jar file if it is signed. |
| mode | the mode in which the file is to be opened |
Throws
| IOException | if an I/O error has occurred |
|---|---|
| IllegalArgumentException | if the mode argument is invalid |
| SecurityException | if access to the file is denied by the SecurityManager |
Public Methods
public Enumeration<JarEntry> entries ()
Returns an enumeration of the zip file entries.
Returns
- an enumeration of the ZIP file entries
public ZipEntry getEntry (String name)
Returns the ZipEntry for the given entry name or
null if not found.
Parameters
| name | the jar file entry name |
|---|
Returns
- the
ZipEntryfor the given entry name ornullif not found
Throws
| IllegalStateException | may be thrown if the jar file has been closed |
|---|
See Also
public synchronized InputStream getInputStream (ZipEntry ze)
Returns an input stream for reading the contents of the specified zip file entry.
Parameters
| ze | the zip file entry |
|---|
Returns
- an input stream for reading the contents of the specified zip file entry
Throws
| ZipException | if a zip file format error has occurred |
|---|---|
| IOException | if an I/O 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 |
public JarEntry getJarEntry (String name)
Returns the JarEntry for the given entry name or
null if not found.
Parameters
| name | the jar file entry name |
|---|
Returns
- the
JarEntryfor the given entry name ornullif not found.
Throws
| IllegalStateException | may be thrown if the jar file has been closed |
|---|
See Also
public Manifest getManifest ()
Returns the jar file manifest, or null if none.
Returns
- the jar file manifest, or
nullif none
Throws
| IllegalStateException | may be thrown if the jar file has been closed |
|---|---|
| IOException | if an I/O error has occurred |