simar
simar

Reputation: 1842

how to get entries from manifest.mf file bundled with jar

i follow this Reading my own Jar's Manifest guidelines to get manifest entries programmatically.

I am building jar file with maven-assembly-plugin. Manifest file is located in META-INF/MANIFEST.MF inside jar file.

Problem is that i am getting empty entry in any case. I tried to load using JarFile, Using ClassLoaderResources not effect.

But if add extra entries into pom file, using then i can read this entries if they are in separate section (separated with extra newline).

If i add entries to main attributes section with then they are invisible.

Is it possible that for some security reason reading programmatically manifest.mf file is disabled?

Upvotes: 0

Views: 1288

Answers (1)

Mark W
Mark W

Reputation: 2803

I dont know why you cant find your manifest entries without additional dependencies, but you shouldn't have any issues reading from a manifest. I do it like this:

private static final String MANIFEST_FILE_KEY = "META-INF/MANIFEST.MF";

......

        try (JarFile jar = new JarFile(URLDecoder.decode(jarFileURL.getPath(), "UTF-8"))) {
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                if (entry.getName().equals(MANIFEST_FILE_KEY)){
                    //found our manifest
                    Manifest manifest = new Manifest(jar.getInputStream(entry));
                    Attributes attributes = manifest.getMainAttributes();
                    for (Entry<Object, Object> key : attributes.entrySet()) {
                        //do something with the key values
                    }
                    break;
                }
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }

Upvotes: 3

Related Questions