Example usage for java.util.jar JarFile getManifest

List of usage examples for java.util.jar JarFile getManifest

Introduction

In this page you can find the example usage for java.util.jar JarFile getManifest.

Prototype

public Manifest getManifest() throws IOException 

Source Link

Document

Returns the jar file manifest, or null if none.

Usage

From source file:com.thoughtworks.go.agent.common.util.JarUtil.java

private static String getManifestKey(JarFile jarFile, String key) {
    try {/* www . j a v  a2 s . c o  m*/
        Manifest manifest = jarFile.getManifest();
        if (manifest != null) {
            Attributes attributes = manifest.getMainAttributes();
            return attributes.getValue(key);
        }
    } catch (IOException e) {
        LOG.error("Exception while trying to read key {} from manifest of {}", key, jarFile.getName(), e);
    }
    return null;
}

From source file:org.apache.hama.monitor.Configurator.java

/**
 * Load jar from specified path.// w  ww .ja v  a  2 s .  c o m
 * 
 * @param path to the jar file.
 * @param loader of the current thread.
 * @return task to be run.
 */
private static Task load(File path, ClassLoader loader) throws IOException {
    JarFile jar = new JarFile(path);
    Manifest manifest = jar.getManifest();
    String pkg = manifest.getMainAttributes().getValue("Package");
    String main = manifest.getMainAttributes().getValue("Main-Class");
    if (null == pkg || null == main)
        throw new NullPointerException("Package or main class not found " + "in menifest file.");
    String namespace = pkg + File.separator + main;
    namespace = namespace.replaceAll(File.separator, ".");
    LOG.debug("Task class to be loaded: " + namespace);
    URLClassLoader child = new URLClassLoader(new URL[] { path.toURI().toURL() }, loader);
    Thread.currentThread().setContextClassLoader(child);
    Class<?> taskClass = null;
    try {
        taskClass = Class.forName(namespace, true, child); // task class
    } catch (ClassNotFoundException cnfe) {
        LOG.warn("Task class is not found.", cnfe);
    }
    if (null == taskClass)
        return null;

    try {
        return (Task) taskClass.newInstance();
    } catch (InstantiationException ie) {
        LOG.warn("Unable to instantiate task class." + namespace, ie);
    } catch (IllegalAccessException iae) {
        LOG.warn(iae);
    }
    return null;
}

From source file:org.terentich.pram.tookit.MetaReader.java

private static Manifest getCurrentJarManifest() {

    Manifest manifest = null;/*ww w  .  j  a  v  a  2s .  c om*/
    CodeSource code = MetaReader.class.getProtectionDomain().getCodeSource();

    try {
        String currentFile = URLDecoder.decode(code.getLocation().getFile(), CharEncoding.UTF_8);

        if (currentFile.endsWith(".jar")) {
            JarFile jar = new JarFile(currentFile);
            manifest = jar.getManifest();
        }
    } catch (IOException e) {
        System.out.println("An exception occurs: " + e);
    }

    return manifest;
}

From source file:tk.tomby.tedit.plugins.PluginDriver.java

private static JarEntry getDescriptor(JarFile jar) throws IOException {
    JarEntry entry = null;/*from w  w  w  .  ja v a  2  s  .  c om*/

    Manifest manifest = jar.getManifest();
    Attributes attrs = manifest.getMainAttributes();
    String pluginAttr = attrs.getValue("Plugin-Descriptor");

    if (pluginAttr != null) {
        entry = jar.getJarEntry(pluginAttr);
    } else {
        entry = jar.getJarEntry("META-INF/plugin.xml");
    }

    return entry;
}

From source file:org.talend.commons.utils.resource.BundleFileUtil.java

public static Manifest getManifest(File f) throws IOException {
    if (f == null || !f.exists() || !f.isFile() || !f.getName().endsWith(FileExtensions.JAR_FILE_SUFFIX)) {
        return null;
    }/*from  w  w w .  jav a2 s .  c  o m*/
    JarFile jarFile = null;
    try {
        jarFile = new JarFile(f);
        return jarFile.getManifest();
    } finally {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:net.fabricmc.installer.installer.ClientInstaller.java

public static void install(File mcDir, String version, IInstallerProgress progress) throws IOException {
    String[] split = version.split("-");
    if (isValidInstallLocation(mcDir, split[0]).isPresent()) {
        throw new RuntimeException(isValidInstallLocation(mcDir, split[0]).get());
    }/*from  w w w. j a  va2 s. c om*/
    File fabricData = new File(mcDir, "fabricData");
    File fabricJar = new File(fabricData, version + ".jar");
    if (!fabricJar.exists()) {
        progress.updateProgress(Translator.getString("install.client.downloadFabric"), 10);
        FileUtils.copyURLToFile(new URL(MavenHandler.getPath(Reference.MAVEN_SERVER_URL,
                Reference.PACKAGE_FABRIC, Reference.NAME_FABRIC_LOADER, version)), fabricJar);
    }
    JarFile jarFile = new JarFile(fabricJar);
    Attributes attributes = jarFile.getManifest().getMainAttributes();
    String mcVersion = attributes.getValue("MinecraftVersion");
    install(mcDir, mcVersion, progress, fabricJar);
    FileUtils.deleteDirectory(fabricData);
}

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

private static void showVersion() {
    try {/*from w ww .j  a  v a  2s . co 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 {//from  ww  w .  ja  va2s.co 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.ow2.chameleon.core.utils.BundleHelper.java

/**
 * Checks whether the given file is a bundle or not.
 * The check is based on the {@literal Bundle-ManifestVersion} header.
 * If the file is a directory this method checks if the directory is an exploded bundle.
 * If the file is a jar file, it checks the manifest.
 *
 * @param file the file./*from   w w w. j  a  v  a  2  s  .co  m*/
 * @return {@literal true} if it's a bundle, {@literal false} otherwise.
 */
public static boolean isBundle(File file) {

    if (file.isFile() && file.getName().endsWith(".jar")) {
        JarFile jar = null;
        try {
            jar = new JarFile(file);
            return jar.getManifest() != null && jar.getManifest().getMainAttributes() != null
                    && jar.getManifest().getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME) != null;
            // We check the symbolic name because it's the only mandatory header
            // see http://wiki.osgi.org/wiki/Bundle-SymbolicName.
        } catch (IOException e) {
            LoggerFactory.getLogger(BundleHelper.class)
                    .error("Cannot check if the file {} is a bundle, " + "cannot open it", file.getName(), e);
            return false;
        } finally {
            final JarFile finalJar = jar;
            IOUtils.closeQuietly(new Closeable() {
                @Override
                public void close() throws IOException {
                    if (finalJar != null) {
                        finalJar.close();
                    }
                }
            });
        }
    }

    return isExplodedBundle(file);

}

From source file:org.springframework.boot.devtools.restart.ChangeableUrls.java

private static List<URL> getUrlsFromManifestClassPathAttribute(URL jarUrl, JarFile jarFile) throws IOException {
    Manifest manifest = jarFile.getManifest();
    if (manifest == null) {
        return Collections.emptyList();
    }/*from  w w  w .  ja v  a 2s.c  om*/
    String classPath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
    if (!StringUtils.hasText(classPath)) {
        return Collections.emptyList();
    }
    String[] entries = StringUtils.delimitedListToStringArray(classPath, " ");
    List<URL> urls = new ArrayList<>(entries.length);
    List<URL> nonExistentEntries = new ArrayList<>();
    for (String entry : entries) {
        try {
            URL referenced = new URL(jarUrl, entry);
            if (new File(referenced.getFile()).exists()) {
                urls.add(referenced);
            } else {
                nonExistentEntries.add(referenced);
            }
        } catch (MalformedURLException ex) {
            throw new IllegalStateException("Class-Path attribute contains malformed URL", ex);
        }
    }
    if (!nonExistentEntries.isEmpty()) {
        System.out.println("The Class-Path manifest attribute in " + jarFile.getName()
                + " referenced one or more files that do not exist: "
                + StringUtils.collectionToCommaDelimitedString(nonExistentEntries));
    }
    return urls;
}