Example usage for java.util.jar Attributes getValue

List of usage examples for java.util.jar Attributes getValue

Introduction

In this page you can find the example usage for java.util.jar Attributes getValue.

Prototype

public String getValue(Name name) 

Source Link

Document

Returns the value of the specified Attributes.Name, or null if the attribute was not found.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JarFile jarfile = new JarFile("filename.jar");

    Manifest manifest = jarfile.getManifest();

    Attributes attrs = (Attributes) manifest.getMainAttributes();

    for (Iterator it = attrs.keySet().iterator(); it.hasNext();) {
        Attributes.Name attrName = (Attributes.Name) it.next();

        String attrValue = attrs.getValue(attrName);
    }// w  w w .jav  a  2 s.  c o m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JarFile jarfile = new JarFile("filename.jar", true);

    Manifest manifest = jarfile.getManifest();

    Attributes attrs = (Attributes) manifest.getMainAttributes();

    for (Iterator it = attrs.keySet().iterator(); it.hasNext();) {
        Attributes.Name attrName = (Attributes.Name) it.next();

        String attrValue = attrs.getValue(attrName);
    }//from w  ww . j  a  v a 2  s  .  c o m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JarFile jarfile = new JarFile(new File("filename.jar"), true);

    Manifest manifest = jarfile.getManifest();

    Attributes attrs = (Attributes) manifest.getMainAttributes();

    for (Iterator it = attrs.keySet().iterator(); it.hasNext();) {
        Attributes.Name attrName = (Attributes.Name) it.next();

        String attrValue = attrs.getValue(attrName);
    }//from ww  w  . j  a  v a  2 s  .c  o m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JarFile jarfile = new JarFile(new File("filename.jar"), true, JarFile.OPEN_READ);

    Manifest manifest = jarfile.getManifest();

    Attributes attrs = (Attributes) manifest.getMainAttributes();

    for (Iterator it = attrs.keySet().iterator(); it.hasNext();) {
        Attributes.Name attrName = (Attributes.Name) it.next();

        String attrValue = attrs.getValue(attrName);
    }// w  w w .j  a v a  2  s . c o m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JarFile jarfile = new JarFile("filename.jar");

    Manifest manifest = jarfile.getManifest();

    Map map = manifest.getEntries();

    for (Iterator it = map.keySet().iterator(); it.hasNext();) {
        String entryName = (String) it.next();

        Attributes attrs = (Attributes) map.get(entryName);

        for (Iterator it2 = attrs.keySet().iterator(); it2.hasNext();) {
            Attributes.Name attrName = (Attributes.Name) it2.next();

            String attrValue = attrs.getValue(attrName);
        }//w w w  . java 2  s. co  m
    }
}

From source file:org.northrop.leanne.publisher.Main.java

public static void main(String[] args) {
    CommandLineParser parser = new GnuParser();
    try {/*from www . j  a va 2s  .c  o m*/
        // parse the command line arguments
        CommandLine line = parser.parse(options, args);

        if (args.length == 0 || line.hasOption("help")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("pub", options);
        } else if (line.hasOption("version")) {
            URLClassLoader cl = (URLClassLoader) Main.class.getClassLoader();
            String title = "";
            String version = "";
            String date = "";
            try {
                URL url = cl.findResource("META-INF/MANIFEST.MF");
                Manifest manifest = new Manifest(url.openStream());
                Attributes attr = manifest.getMainAttributes();
                title = attr.getValue("Implementation-Title");
                version = attr.getValue("Implementation-Version");
                date = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM)
                        .format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(attr.getValue("Built-Date")));
            } catch (Exception e) {
                e.printStackTrace();
            }

            System.out.println("------------------------------------------------------------");
            System.out.println("Publisher Pipeline " + version);
            System.out.println("------------------------------------------------------------");
            System.out.println("");
            System.out.println(title + " build time " + date);
            System.out.println("Java: " + System.getProperty("java.version"));
            System.out.println("JVM:  " + System.getProperty("java.vendor"));
            System.out.println("OS:   " + System.getProperty("os.name") + " " + System.getProperty("os.version")
                    + " " + System.getProperty("os.arch"));
        } else {
            Option[] options = line.getOptions();
            Binding binding = new Binding();
            binding.setVariable("home", System.getProperty("pub.home"));
            binding.setVariable("inputDir", System.getProperty("pub.home") + "/input");
            binding.setVariable("outputDir", System.getProperty("pub.home") + "/output");
            binding.setVariable("appName", System.getProperty("program.name"));
            binding.setVariable("isdebug", false);

            for (int i = 0; i < options.length; i++) {
                Option opt = options[i];
                binding.setVariable("is" + opt.getOpt(), true);
            }

            String[] roots = new String[] { System.getProperty("pub.home") + "/resources/scripts",
                    System.getProperty("pub.home") + "/resources/scripts/formats" };
            ClassLoader parent = Main.class.getClassLoader();
            GroovyScriptEngine gse = new GroovyScriptEngine(roots, parent);

            if (!line.hasOption("rerun")) {
                gse.run("prep.groovy", binding);
            }

            for (String name : getFormats()) {
                if (line.hasOption(name.toLowerCase())) {
                    String file = ("" + name.charAt(0)).toLowerCase() + name.substring(1) + ".groovy";
                    gse.run(file, binding);
                }
            }
        }
    } catch (ParseException exp) {
        System.err.println("Command line parsing failed.  Reason: " + exp.getMessage());
    } catch (ResourceException resourceError) {
        System.err.println("Groovy script failed.  Reason: " + resourceError.getMessage());
    } catch (IOException ioError) {
        System.err.println("Groovy script failed.  Reason: " + ioError.getMessage());
    } catch (ScriptException error) {
        System.err.println("Groovy script failed.  Reason: " + error.getMessage());
        error.printStackTrace();
    }
}

From source file:edu.cornell.med.icb.util.VersionUtils.java

/**
 * Gets the Implementation-Version attribute from the manifest of the jar file a class
 * is loaded from.//from   ww w. j  a va2s .  c  o  m
 * @param clazz The class to get the version for
 * @return The value of the Implementation-Version attribute or "UNKNOWN" if the
 * jar file cannot be read.
 */
public static String getImplementationVersion(final Class<?> clazz) {
    String version;
    try {
        final String classContainer = clazz.getProtectionDomain().getCodeSource().getLocation().toString();
        final URL manifestUrl = new URL("jar:" + classContainer + "!/" + JarFile.MANIFEST_NAME);
        final Manifest manifest = new Manifest(manifestUrl.openStream());
        final Attributes attributes = manifest.getMainAttributes();
        version = attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
    } catch (Exception e) {
        // pretty much any error here is ok since we may not even have a jar to read from
        version = "UNKNOWN";
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug(Attributes.Name.IMPLEMENTATION_VERSION + ": " + version);
    }
    return StringUtils.defaultString(version);
}

From source file:edu.cmu.tetrad.cli.TetradCliApp.java

private static void showVersion() {
    try {/*from   w w  w  . j a  v  a 2  s .c o  m*/
        JarFile jarFile = new JarFile(
                TetradCliApp.class.getProtectionDomain().getCodeSource().getLocation().getPath(), true);
        Manifest manifest = jarFile.getManifest();
        Attributes attributes = manifest.getMainAttributes();
        String artifactId = attributes.getValue("Implementation-Title");
        String version = attributes.getValue("Implementation-Version");
        System.out.printf("%s version: %s%n", artifactId, version);
    } catch (IOException exception) {
        String errMsg = "Unable to retrieve version number.";
        System.err.println(errMsg);
        LOGGER.error(errMsg, exception);
    }
}

From source file:edu.cmu.tetrad.cli.TetradCliApp.java

private static void showHelp() {
    String cmdLineSyntax = "java -jar ";
    try {//  w  w w.j  a va 2  s.  c o m
        JarFile jarFile = new JarFile(
                TetradCliApp.class.getProtectionDomain().getCodeSource().getLocation().getPath(), true);
        Manifest manifest = jarFile.getManifest();
        Attributes attributes = manifest.getMainAttributes();
        String artifactId = attributes.getValue("Implementation-Title");
        String version = attributes.getValue("Implementation-Version");
        cmdLineSyntax += String.format("%s-%s.jar", artifactId, version);
    } catch (IOException exception) {
        cmdLineSyntax += "causal-cmd.jar";
    }

    HelpFormatter formatter = new HelpFormatter();
    formatter.printHelp(cmdLineSyntax, MAIN_OPTIONS, true);
}

From source file:org.eclipse.gemini.blueprint.test.internal.util.jar.ManifestUtils.java

/**
 * Determine the Import-Package value based on the Export-Package entries in
 * the jars given as Resources.// w  w w.java  2 s  .  c  o m
 * @param resources
 * @return
 */
public static String[] determineImportPackages(Resource[] resources) {
    Set collection = new LinkedHashSet();
    // for each resource
    for (int i = 0; i < resources.length; i++) {
        Resource resource = resources[i];
        Manifest man = JarUtils.getManifest(resource);
        if (man != null) {
            // read the manifest
            // get the Export-Package
            Attributes attrs = man.getMainAttributes();
            String exportedPackages = attrs.getValue(Constants.EXPORT_PACKAGE);
            // add it to the StringBuilder
            if (StringUtils.hasText(exportedPackages)) {
                collection.addAll(StringUtils.commaDelimitedListToSet(exportedPackages));
            }
        }
    }
    // return the result as string
    String[] array = (String[]) collection.toArray(new String[collection.size()]);

    // clean whitespace just in case
    for (int i = 0; i < array.length; i++) {
        array[i] = StringUtils.trimWhitespace(array[i]);
    }
    return array;
}