get Manifest Property - Java Reflection

Java examples for Reflection:Jar

Description

get Manifest Property

Demo Code


//package com.java2s;

import java.io.IOException;
import java.io.InputStream;

import java.util.jar.JarFile;
import java.util.jar.Manifest;

import java.util.zip.ZipEntry;

public class Main {
    public static String getManifestProperty(String jarFilename,
            String manifestKey) throws IOException {
        try (JarFile file = new JarFile(jarFilename)) {
            ZipEntry entry = file.getEntry("META-INF/MANIFEST.MF");
            InputStream is = file.getInputStream(entry);
            if (is == null) {
                return "(No META-INF/MANIFEST.MF file)";
            }//from w w  w. j  av  a2  s  .com
            return readManifestProperty(is, manifestKey);
        }
    }

    public static String getManifestProperty(
            ClassLoader clContainingManifest, String manifestKey)
            throws IOException {
        // Check Manifest file for generated build number
        InputStream is = clContainingManifest
                .getResourceAsStream("META-INF/MANIFEST.MF");
        return readManifestProperty(is, manifestKey);
    }

    private static String readManifestProperty(InputStream is,
            String manifestKey) throws IOException {
        Manifest mf = new Manifest();
        if (is == null) {
            return "(No META-INF/MANIFEST.MF file)";
        }
        mf.read(is);
        return mf.getMainAttributes().getValue(manifestKey);
    }
}

Related Tutorials