java.util.jar.Attributes#containsKey ( )源码实例Demo

下面列出了java.util.jar.Attributes#containsKey ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。


private Manifest jgitverManifestOrEmpty() {
    try {
        Enumeration<URL> resources = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            Manifest manifest = new Manifest(url.openStream());
            Attributes mfAttrs = manifest.getMainAttributes();
            Attributes.Name attributeName = new Attributes.Name(X_MAVEN_COORDINATES);
            if (mfAttrs.containsKey(attributeName) && mfAttrs.getValue(attributeName).startsWith("fr.brouillard.oss:jgitver")) {
                return manifest;
            }
        }
    } catch (IOException ignore) {
        // expected
    }
    return new Manifest();
}
 
源代码2 项目: dragonwell8_jdk   文件: LauncherHelper.java

static String getMainClassFromJar(String jarname) {
    String mainValue = null;
    try (JarFile jarFile = new JarFile(jarname)) {
        Manifest manifest = jarFile.getManifest();
        if (manifest == null) {
            abort(null, "java.launcher.jar.error2", jarname);
        }
        Attributes mainAttrs = manifest.getMainAttributes();
        if (mainAttrs == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }
        mainValue = mainAttrs.getValue(MAIN_CLASS);
        if (mainValue == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }

        /*
         * Hand off to FXHelper if it detects a JavaFX application
         * This must be done after ensuring a Main-Class entry
         * exists to enforce compliance with the jar specification
         */
        if (mainAttrs.containsKey(
                new Attributes.Name(FXHelper.JAVAFX_APPLICATION_MARKER))) {
            return FXHelper.class.getName();
        }

        return mainValue.trim();
    } catch (IOException ioe) {
        abort(ioe, "java.launcher.jar.error1", jarname);
    }
    return null;
}
 
源代码3 项目: TencentKona-8   文件: LauncherHelper.java

static String getMainClassFromJar(String jarname) {
    String mainValue = null;
    try (JarFile jarFile = new JarFile(jarname)) {
        Manifest manifest = jarFile.getManifest();
        if (manifest == null) {
            abort(null, "java.launcher.jar.error2", jarname);
        }
        Attributes mainAttrs = manifest.getMainAttributes();
        if (mainAttrs == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }
        mainValue = mainAttrs.getValue(MAIN_CLASS);
        if (mainValue == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }

        /*
         * Hand off to FXHelper if it detects a JavaFX application
         * This must be done after ensuring a Main-Class entry
         * exists to enforce compliance with the jar specification
         */
        if (mainAttrs.containsKey(
                new Attributes.Name(FXHelper.JAVAFX_APPLICATION_MARKER))) {
            return FXHelper.class.getName();
        }

        return mainValue.trim();
    } catch (IOException ioe) {
        abort(ioe, "java.launcher.jar.error1", jarname);
    }
    return null;
}
 
源代码4 项目: jdk8u60   文件: LauncherHelper.java

static String getMainClassFromJar(String jarname) {
    String mainValue = null;
    try (JarFile jarFile = new JarFile(jarname)) {
        Manifest manifest = jarFile.getManifest();
        if (manifest == null) {
            abort(null, "java.launcher.jar.error2", jarname);
        }
        Attributes mainAttrs = manifest.getMainAttributes();
        if (mainAttrs == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }
        mainValue = mainAttrs.getValue(MAIN_CLASS);
        if (mainValue == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }

        /*
         * Hand off to FXHelper if it detects a JavaFX application
         * This must be done after ensuring a Main-Class entry
         * exists to enforce compliance with the jar specification
         */
        if (mainAttrs.containsKey(
                new Attributes.Name(FXHelper.JAVAFX_APPLICATION_MARKER))) {
            return FXHelper.class.getName();
        }

        return mainValue.trim();
    } catch (IOException ioe) {
        abort(ioe, "java.launcher.jar.error1", jarname);
    }
    return null;
}
 
源代码5 项目: openjdk-jdk8u   文件: LauncherHelper.java

static String getMainClassFromJar(String jarname) {
    String mainValue = null;
    try (JarFile jarFile = new JarFile(jarname)) {
        Manifest manifest = jarFile.getManifest();
        if (manifest == null) {
            abort(null, "java.launcher.jar.error2", jarname);
        }
        Attributes mainAttrs = manifest.getMainAttributes();
        if (mainAttrs == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }
        mainValue = mainAttrs.getValue(MAIN_CLASS);
        if (mainValue == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }

        /*
         * Hand off to FXHelper if it detects a JavaFX application
         * This must be done after ensuring a Main-Class entry
         * exists to enforce compliance with the jar specification
         */
        if (mainAttrs.containsKey(
                new Attributes.Name(FXHelper.JAVAFX_APPLICATION_MARKER))) {
            return FXHelper.class.getName();
        }

        return mainValue.trim();
    } catch (IOException ioe) {
        abort(ioe, "java.launcher.jar.error1", jarname);
    }
    return null;
}
 
源代码6 项目: snap-desktop   文件: ModulePackager.java

public static String getAdapterVersion(File jarFile) throws IOException {
    String version = null;
    JarFile jar = new JarFile(jarFile);
    Attributes attributes = jar.getManifest().getMainAttributes();
    if (attributes.containsKey(ATTR_MODULE_IMPLEMENTATION)) {
        version = attributes.getValue(ATTR_MODULE_IMPLEMENTATION);
    }
    jar.close();
    return version;
}
 

static String getMainClassFromJar(String jarname) {
    String mainValue = null;
    try (JarFile jarFile = new JarFile(jarname)) {
        Manifest manifest = jarFile.getManifest();
        if (manifest == null) {
            abort(null, "java.launcher.jar.error2", jarname);
        }
        Attributes mainAttrs = manifest.getMainAttributes();
        if (mainAttrs == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }
        mainValue = mainAttrs.getValue(MAIN_CLASS);
        if (mainValue == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }

        /*
         * Hand off to FXHelper if it detects a JavaFX application
         * This must be done after ensuring a Main-Class entry
         * exists to enforce compliance with the jar specification
         */
        if (mainAttrs.containsKey(
                new Attributes.Name(FXHelper.JAVAFX_APPLICATION_MARKER))) {
            return FXHelper.class.getName();
        }

        return mainValue.trim();
    } catch (IOException ioe) {
        abort(ioe, "java.launcher.jar.error1", jarname);
    }
    return null;
}
 
源代码8 项目: jdk8u-jdk   文件: LauncherHelper.java

static String getMainClassFromJar(String jarname) {
    String mainValue = null;
    try (JarFile jarFile = new JarFile(jarname)) {
        Manifest manifest = jarFile.getManifest();
        if (manifest == null) {
            abort(null, "java.launcher.jar.error2", jarname);
        }
        Attributes mainAttrs = manifest.getMainAttributes();
        if (mainAttrs == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }
        mainValue = mainAttrs.getValue(MAIN_CLASS);
        if (mainValue == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }

        /*
         * Hand off to FXHelper if it detects a JavaFX application
         * This must be done after ensuring a Main-Class entry
         * exists to enforce compliance with the jar specification
         */
        if (mainAttrs.containsKey(
                new Attributes.Name(FXHelper.JAVAFX_APPLICATION_MARKER))) {
            return FXHelper.class.getName();
        }

        return mainValue.trim();
    } catch (IOException ioe) {
        abort(ioe, "java.launcher.jar.error1", jarname);
    }
    return null;
}
 
源代码9 项目: hottub   文件: LauncherHelper.java

static String getMainClassFromJar(String jarname) {
    String mainValue = null;
    try (JarFile jarFile = new JarFile(jarname)) {
        Manifest manifest = jarFile.getManifest();
        if (manifest == null) {
            abort(null, "java.launcher.jar.error2", jarname);
        }
        Attributes mainAttrs = manifest.getMainAttributes();
        if (mainAttrs == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }
        mainValue = mainAttrs.getValue(MAIN_CLASS);
        if (mainValue == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }

        /*
         * Hand off to FXHelper if it detects a JavaFX application
         * This must be done after ensuring a Main-Class entry
         * exists to enforce compliance with the jar specification
         */
        if (mainAttrs.containsKey(
                new Attributes.Name(FXHelper.JAVAFX_APPLICATION_MARKER))) {
            return FXHelper.class.getName();
        }

        return mainValue.trim();
    } catch (IOException ioe) {
        abort(ioe, "java.launcher.jar.error1", jarname);
    }
    return null;
}
 
源代码10 项目: snap-desktop   文件: ModulePackager.java

public static String getAdapterAlias(File jarFile) throws IOException {
    String version = null;
    JarFile jar = new JarFile(jarFile);
    Attributes attributes = jar.getManifest().getMainAttributes();
    if (attributes.containsKey(ATTR_MODULE_ALIAS)) {
        version = attributes.getValue(ATTR_MODULE_ALIAS);
    }
    jar.close();
    return version;
}
 
源代码11 项目: openjdk-8   文件: LauncherHelper.java

static String getMainClassFromJar(String jarname) {
    String mainValue = null;
    try (JarFile jarFile = new JarFile(jarname)) {
        Manifest manifest = jarFile.getManifest();
        if (manifest == null) {
            abort(null, "java.launcher.jar.error2", jarname);
        }
        Attributes mainAttrs = manifest.getMainAttributes();
        if (mainAttrs == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }
        mainValue = mainAttrs.getValue(MAIN_CLASS);
        if (mainValue == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }

        /*
         * Hand off to FXHelper if it detects a JavaFX application
         * This must be done after ensuring a Main-Class entry
         * exists to enforce compliance with the jar specification
         */
        if (mainAttrs.containsKey(
                new Attributes.Name(FXHelper.JAVAFX_APPLICATION_MARKER))) {
            return FXHelper.class.getName();
        }

        return mainValue.trim();
    } catch (IOException ioe) {
        abort(ioe, "java.launcher.jar.error1", jarname);
    }
    return null;
}
 
源代码12 项目: jdk8u_jdk   文件: LauncherHelper.java

static String getMainClassFromJar(String jarname) {
    String mainValue = null;
    try (JarFile jarFile = new JarFile(jarname)) {
        Manifest manifest = jarFile.getManifest();
        if (manifest == null) {
            abort(null, "java.launcher.jar.error2", jarname);
        }
        Attributes mainAttrs = manifest.getMainAttributes();
        if (mainAttrs == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }
        mainValue = mainAttrs.getValue(MAIN_CLASS);
        if (mainValue == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }

        /*
         * Hand off to FXHelper if it detects a JavaFX application
         * This must be done after ensuring a Main-Class entry
         * exists to enforce compliance with the jar specification
         */
        if (mainAttrs.containsKey(
                new Attributes.Name(FXHelper.JAVAFX_APPLICATION_MARKER))) {
            return FXHelper.class.getName();
        }

        return mainValue.trim();
    } catch (IOException ioe) {
        abort(ioe, "java.launcher.jar.error1", jarname);
    }
    return null;
}
 
源代码13 项目: jeddict   文件: LibraryUtil.java

public static String getLibrary(ClassPath classPath, String resource) {
    // Find as resource as a class
    String classNameAsPath = resource.replace('.', '/') + ".class";
    FileObject classResource = classPath.findResource(classNameAsPath);
    if (classResource == null) {
        // Find as resource as a package
        classNameAsPath = resource.replace('.', '/');
        classResource = classPath.findResource(classNameAsPath);

    }
    if (classResource != null) {
        FileObject archiveFile = FileUtil.getArchiveFile(classResource);
        if (archiveFile != null && FileUtil.isArchiveFile(archiveFile)) {
            File toFile = FileUtil.toFile(archiveFile);
            try {
                JarFile jf = new JarFile(toFile);
                Manifest manifest = jf.getManifest();
                Attributes mainAttributes = manifest.getMainAttributes();

                // Find library version by Bundle information (Eclipse/OSGi)
                if (mainAttributes.containsKey(BUNDLE_NAME) && mainAttributes.containsKey(BUNDLE_VERSION)) {
                    if (!mainAttributes.getValue(BUNDLE_NAME).isEmpty()) {
                        return mainAttributes.getValue(BUNDLE_NAME);
                    }
                }

                // If unsuccessful, try by default Manifest Headers
                if (mainAttributes.containsKey(Name.IMPLEMENTATION_TITLE) && mainAttributes.containsKey(Name.IMPLEMENTATION_VERSION)) {
                    if (!mainAttributes.getValue(Name.IMPLEMENTATION_TITLE).isEmpty()) {
                        return mainAttributes.getValue(Name.IMPLEMENTATION_TITLE);
                    }
                }
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }

    }
    return null;
}
 
源代码14 项目: jdk8u-jdk   文件: LauncherHelper.java

static String getMainClassFromJar(String jarname) {
    String mainValue = null;
    try (JarFile jarFile = new JarFile(jarname)) {
        Manifest manifest = jarFile.getManifest();
        if (manifest == null) {
            abort(null, "java.launcher.jar.error2", jarname);
        }
        Attributes mainAttrs = manifest.getMainAttributes();
        if (mainAttrs == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }
        mainValue = mainAttrs.getValue(MAIN_CLASS);
        if (mainValue == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }

        /*
         * Hand off to FXHelper if it detects a JavaFX application
         * This must be done after ensuring a Main-Class entry
         * exists to enforce compliance with the jar specification
         */
        if (mainAttrs.containsKey(
                new Attributes.Name(FXHelper.JAVAFX_APPLICATION_MARKER))) {
            return FXHelper.class.getName();
        }

        return mainValue.trim();
    } catch (IOException ioe) {
        abort(ioe, "java.launcher.jar.error1", jarname);
    }
    return null;
}
 
源代码15 项目: jdk8u-dev-jdk   文件: LauncherHelper.java

static String getMainClassFromJar(String jarname) {
    String mainValue = null;
    try (JarFile jarFile = new JarFile(jarname)) {
        Manifest manifest = jarFile.getManifest();
        if (manifest == null) {
            abort(null, "java.launcher.jar.error2", jarname);
        }
        Attributes mainAttrs = manifest.getMainAttributes();
        if (mainAttrs == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }
        mainValue = mainAttrs.getValue(MAIN_CLASS);
        if (mainValue == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }

        /*
         * Hand off to FXHelper if it detects a JavaFX application
         * This must be done after ensuring a Main-Class entry
         * exists to enforce compliance with the jar specification
         */
        if (mainAttrs.containsKey(
                new Attributes.Name(FXHelper.JAVAFX_APPLICATION_MARKER))) {
            return FXHelper.class.getName();
        }

        return mainValue.trim();
    } catch (IOException ioe) {
        abort(ioe, "java.launcher.jar.error1", jarname);
    }
    return null;
}
 
源代码16 项目: jeka   文件: JkManifest.java

/**
 * Returns <code>true</code> if this manifest has no entry or has only
 * "Manifest-Version" entry.
 */
public boolean isEmpty() {
    final Attributes mainAttributes = manifest.getMainAttributes();
    if (mainAttributes.size() > 1) {
        return false;
    }
    if (mainAttributes.size() == 1
            && !mainAttributes.containsKey(Attributes.Name.MANIFEST_VERSION)) {
        return false;
    }
    return manifest.getEntries().size() == 0;
}
 
源代码17 项目: Bytecoder   文件: LauncherHelper.java

static String getMainClassFromJar(String jarname) {
    String mainValue;
    try (JarFile jarFile = new JarFile(jarname)) {
        Manifest manifest = jarFile.getManifest();
        if (manifest == null) {
            abort(null, "java.launcher.jar.error2", jarname);
        }
        Attributes mainAttrs = manifest.getMainAttributes();
        if (mainAttrs == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }

        // Main-Class
        mainValue = mainAttrs.getValue(MAIN_CLASS);
        if (mainValue == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }

        // Launcher-Agent-Class (only check for this when Main-Class present)
        String agentClass = mainAttrs.getValue(LAUNCHER_AGENT_CLASS);
        if (agentClass != null) {
            ModuleLayer.boot().findModule("java.instrument").ifPresent(m -> {
                try {
                    String cn = "sun.instrument.InstrumentationImpl";
                    Class<?> clazz = Class.forName(cn, false, null);
                    Method loadAgent = clazz.getMethod("loadAgent", String.class);
                    loadAgent.invoke(null, jarname);
                } catch (Throwable e) {
                    if (e instanceof InvocationTargetException) e = e.getCause();
                    abort(e, "java.launcher.jar.error4", jarname);
                }
            });
        }

        // Add-Exports and Add-Opens
        String exports = mainAttrs.getValue(ADD_EXPORTS);
        if (exports != null) {
            addExportsOrOpens(exports, false);
        }
        String opens = mainAttrs.getValue(ADD_OPENS);
        if (opens != null) {
            addExportsOrOpens(opens, true);
        }

        /*
         * Hand off to FXHelper if it detects a JavaFX application
         * This must be done after ensuring a Main-Class entry
         * exists to enforce compliance with the jar specification
         */
        if (mainAttrs.containsKey(
                new Attributes.Name(JAVAFX_APPLICATION_MARKER))) {
            FXHelper.setFXLaunchParameters(jarname, LM_JAR);
            return FXHelper.class.getName();
        }

        return mainValue.trim();
    } catch (IOException ioe) {
        abort(ioe, "java.launcher.jar.error1", jarname);
    }
    return null;
}
 
源代码18 项目: openjdk-jdk9   文件: LauncherHelper.java

static String getMainClassFromJar(String jarname) {
    String mainValue;
    try (JarFile jarFile = new JarFile(jarname)) {
        Manifest manifest = jarFile.getManifest();
        if (manifest == null) {
            abort(null, "java.launcher.jar.error2", jarname);
        }
        Attributes mainAttrs = manifest.getMainAttributes();
        if (mainAttrs == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }

        // Main-Class
        mainValue = mainAttrs.getValue(MAIN_CLASS);
        if (mainValue == null) {
            abort(null, "java.launcher.jar.error3", jarname);
        }

        // Launcher-Agent-Class (only check for this when Main-Class present)
        String agentClass = mainAttrs.getValue(LAUNCHER_AGENT_CLASS);
        if (agentClass != null) {
            ModuleLayer.boot().findModule("java.instrument").ifPresent(m -> {
                try {
                    String cn = "sun.instrument.InstrumentationImpl";
                    Class<?> clazz = Class.forName(cn, false, null);
                    Method loadAgent = clazz.getMethod("loadAgent", String.class);
                    loadAgent.invoke(null, jarname);
                } catch (Throwable e) {
                    if (e instanceof InvocationTargetException) e = e.getCause();
                    abort(e, "java.launcher.jar.error4", jarname);
                }
            });
        }

        // Add-Exports and Add-Opens
        String exports = mainAttrs.getValue(ADD_EXPORTS);
        if (exports != null) {
            addExportsOrOpens(exports, false);
        }
        String opens = mainAttrs.getValue(ADD_OPENS);
        if (opens != null) {
            addExportsOrOpens(opens, true);
        }

        /*
         * Hand off to FXHelper if it detects a JavaFX application
         * This must be done after ensuring a Main-Class entry
         * exists to enforce compliance with the jar specification
         */
        if (mainAttrs.containsKey(
                new Attributes.Name(JAVAFX_APPLICATION_MARKER))) {
            FXHelper.setFXLaunchParameters(jarname, LM_JAR);
            return FXHelper.class.getName();
        }

        return mainValue.trim();
    } catch (IOException ioe) {
        abort(ioe, "java.launcher.jar.error1", jarname);
    }
    return null;
}
 

private static MCRComponent buildComponent(Manifest manifest, URL manifestURL) throws IOException {
    Attributes mainAttributes = manifest.getMainAttributes();
    String artifactId = mainAttributes.getValue(ATT_MCR_ARTIFACT_ID);
    String pomPropertiesPath = mainAttributes.getValue(ATT_POM);
    boolean usePomProperties = false;

    if (artifactId == null) {
        if (!mainAttributes.containsKey(ATT_POM)) {
            return null;
        }

        if (pomPropertiesPath == null) {
            return null;
        }

        try (InputStream pi = MCRClassTools.getClassLoader().getResourceAsStream(
            pomPropertiesPath)) {
            if (pi == null) {
                LOGGER.warn("Manifest entry {} set to \"{}\", but resource could not be loaded.", ATT_POM,
                    pomPropertiesPath);
                return null;
            }
            Properties pomProperties = new Properties();
            pomProperties.load(pi);
            artifactId = (String) pomProperties.get("artifactId");
            usePomProperties = true;
        }
    }

    if (artifactId != null && artifactId.startsWith("mycore-")
        || mainAttributes.containsKey(ATT_MCR_APPLICATION_MODULE)) {
        if (usePomProperties) {
            LOGGER.warn("No Attribute \"{}\" in Manifest of {}.", ATT_MCR_ARTIFACT_ID,
                mainAttributes.getValue(ATT_MCR_APPLICATION_MODULE));
            LOGGER.warn("Change this in the future, pom.properties path definition is deprecated.");
            LOGGER.info("Using artifactId in {}.", pomPropertiesPath);
        }

        return new MCRComponent(artifactId, manifest, extractJarFile(manifestURL));
    }
    return null;
}
 

private static boolean isValidBundle(Manifest manifest) {
    Attributes atts = manifest.getMainAttributes();
    return atts.containsKey(new Attributes.Name(Constants.BUNDLE_MANIFESTVERSION));
}