Home > Java > javaTutorial > How to Access the Manifest File of the Originating JAR in Applet or Webstart Environments?

How to Access the Manifest File of the Originating JAR in Applet or Webstart Environments?

DDD
Release: 2024-11-19 13:45:02
Original
297 people have browsed it

How to Access the Manifest File of the Originating JAR in Applet or Webstart Environments?

Retrieving the Manifest File of the Originating JAR

To access the Manifest file of the JAR that initiated your application, conventional methods like getClass().getClassLoader().getResources(...) may not suffice, especially in environments like applets or webstart applications. Here are two alternative approaches to consider:

Iterating through Retrieved URLs:

  1. Use getResources() to retrieve a collection of URLs representing potential Manifest files.
  2. Iterate through these URLs, reading each one as a Manifest until you locate the one you need.

Example Code:

Enumeration<URL> resources = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
while (resources.hasMoreElements()) {
    try {
        URL url = resources.nextElement();
        Manifest manifest = new Manifest(url.openStream());
        // If manifest is null, try using JarInputStream instead: manifest = url.openStream().getManifest();

        // Verify and process the manifest as needed
        ...
    } catch (IOException e) {
        // Handle the exception
    }
}
Copy after login

Checking ClassLoader Type and Using findResource():

  1. Determine if getClass().getClassLoader() is an instance of java.net.URLClassLoader, typically the case for Sun classloaders, including AppletClassLoader.
  2. If so, cast it and use findResource() to retrieve the Manifest.

Example Code:

URLClassLoader cl = (URLClassLoader) getClass().getClassLoader();
try {
    URL url = cl.findResource("META-INF/MANIFEST.MF");
    Manifest manifest = new Manifest(url.openStream());
    // Do your stuff with the manifest
    ...
} catch (IOException e) {
    // Handle the exception
}
Copy after login

The above is the detailed content of How to Access the Manifest File of the Originating JAR in Applet or Webstart Environments?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template