List of usage examples for java.util.jar JarFile JarFile
public JarFile(File file, boolean verify) throws IOException
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 w w.jav a 2 s . c om }
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); }//w ww . j a v a2 s. co m }
From source file:edu.cmu.tetrad.cli.TetradCliApp.java
private static void showVersion() { try {/*from ww w. j a va2 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 {/*from w ww . ja va2 s . c om*/ 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.red5.server.plugin.PluginLauncher.java
public void afterPropertiesSet() throws Exception { ApplicationContext common = (ApplicationContext) applicationContext.getBean("red5.common"); Server server = (Server) common.getBean("red5.server"); //server should be up and running at this point so load any plug-ins now //get the plugins dir File pluginsDir = new File(System.getProperty("red5.root"), "plugins"); File[] plugins = pluginsDir.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { //lower the case String tmp = name.toLowerCase(); //accept jars and zips return tmp.endsWith(".jar") || tmp.endsWith(".zip"); }/*from w w w. ja v a 2 s . c o m*/ }); if (plugins != null) { IRed5Plugin red5Plugin = null; log.debug("{} plugins to launch", plugins.length); for (File plugin : plugins) { JarFile jar = null; Manifest manifest = null; try { jar = new JarFile(plugin, false); manifest = jar.getManifest(); } catch (Exception e1) { log.warn("Error loading plugin manifest: {}", plugin); } finally { jar.close(); } if (manifest == null) { continue; } Attributes attributes = manifest.getMainAttributes(); if (attributes == null) { continue; } String pluginMainClass = attributes.getValue("Red5-Plugin-Main-Class"); if (pluginMainClass == null || pluginMainClass.length() <= 0) { continue; } // attempt to load the class; since it's in the plugins directory this should work ClassLoader loader = common.getClassLoader(); Class<?> pluginClass; String pluginMainMethod = null; try { pluginClass = Class.forName(pluginMainClass, true, loader); } catch (ClassNotFoundException e) { continue; } try { //handle plug-ins without "main" methods pluginMainMethod = attributes.getValue("Red5-Plugin-Main-Method"); if (pluginMainMethod == null || pluginMainMethod.length() <= 0) { //just get an instance of the class red5Plugin = (IRed5Plugin) pluginClass.newInstance(); } else { Method method = pluginClass.getMethod(pluginMainMethod, (Class<?>[]) null); Object o = method.invoke(null, (Object[]) null); if (o != null && o instanceof IRed5Plugin) { red5Plugin = (IRed5Plugin) o; } } //register and start if (red5Plugin != null) { //set top-level context red5Plugin.setApplicationContext(applicationContext); //set server reference red5Plugin.setServer(server); //register the plug-in to make it available for lookups PluginRegistry.register(red5Plugin); //start the plugin red5Plugin.doStart(); } log.info("Loaded plugin: {}", pluginMainClass); } catch (Throwable t) { log.warn("Error loading plugin: {}; Method: {}", pluginMainClass, pluginMainMethod); log.error("", t); } } } else { log.info("Plugins directory cannot be accessed or doesnt exist"); } }
From source file:org.apache.maven.shared.osgi.DefaultMaven2OsgiConverter.java
/** * Get the symbolic name as groupId + "." + artifactId, with the following exceptions * <ul>//from w ww. jav a 2 s . c om * <li>if artifact.getFile is not null and the jar contains a OSGi Manifest with * Bundle-SymbolicName property then that value is returned</li> * <li>if groupId has only one section (no dots) and artifact.getFile is not null then the * first package name with classes is returned. eg. commons-logging:commons-logging -> * org.apache.commons.logging</li> * <li>if artifactId is equal to last section of groupId then groupId is returned. eg. * org.apache.maven:maven -> org.apache.maven</li> * <li>if artifactId starts with last section of groupId that portion is removed. eg. * org.apache.maven:maven-core -> org.apache.maven.core</li> * </ul> */ public String getBundleSymbolicName(Artifact artifact) { if ((artifact.getFile() != null) && artifact.getFile().isFile()) { Analyzer analyzer = new Analyzer(); JarFile jar = null; try { jar = new JarFile(artifact.getFile(), false); if (jar.getManifest() != null) { String symbolicNameAttribute = jar.getManifest().getMainAttributes() .getValue(Analyzer.BUNDLE_SYMBOLICNAME); Map bundleSymbolicNameHeader = analyzer.parseHeader(symbolicNameAttribute); Iterator it = bundleSymbolicNameHeader.keySet().iterator(); if (it.hasNext()) { return (String) it.next(); } } } catch (IOException e) { throw new ManifestReadingException( "Error reading manifest in jar " + artifact.getFile().getAbsolutePath(), e); } finally { if (jar != null) { try { jar.close(); } catch (IOException e) { } } } } int i = artifact.getGroupId().lastIndexOf('.'); if ((i < 0) && (artifact.getFile() != null) && artifact.getFile().isFile()) { String groupIdFromPackage = getGroupIdFromPackage(artifact.getFile()); if (groupIdFromPackage != null) { return groupIdFromPackage; } } String lastSection = artifact.getGroupId().substring(++i); if (artifact.getArtifactId().equals(lastSection)) { return artifact.getGroupId(); } if (artifact.getArtifactId().startsWith(lastSection)) { String artifactId = artifact.getArtifactId().substring(lastSection.length()); if (Character.isLetterOrDigit(artifactId.charAt(0))) { return getBundleSymbolicName(artifact.getGroupId(), artifactId); } else { return getBundleSymbolicName(artifact.getGroupId(), artifactId.substring(1)); } } return getBundleSymbolicName(artifact.getGroupId(), artifact.getArtifactId()); }
From source file:com.liferay.ide.maven.core.util.DefaultMaven2OsgiConverter.java
/** * Get the symbolic name as groupId + "." + artifactId, with the following exceptions * <ul>//ww w . j a v a 2s .c o m * <li>if artifact.getFile is not null and the jar contains a OSGi Manifest with * Bundle-SymbolicName property then that value is returned</li> * <li>if groupId has only one section (no dots) and artifact.getFile is not null then the * first package name with classes is returned. eg. commons-logging:commons-logging -> * org.apache.commons.logging</li> * <li>if artifactId is equal to last section of groupId then groupId is returned. eg. * org.apache.maven:maven -> org.apache.maven</li> * <li>if artifactId starts with last section of groupId that portion is removed. eg. * org.apache.maven:maven-core -> org.apache.maven.core</li> * </ul> */ public String getBundleSymbolicName(Artifact artifact) { if ((artifact.getFile() != null) && artifact.getFile().exists()) { Analyzer analyzer = new Analyzer(); try { JarFile jar = new JarFile(artifact.getFile(), false); if (jar.getManifest() != null) { String symbolicNameAttribute = jar.getManifest().getMainAttributes() .getValue(Analyzer.BUNDLE_SYMBOLICNAME); Map bundleSymbolicNameHeader = analyzer.parseHeader(symbolicNameAttribute); Iterator it = bundleSymbolicNameHeader.keySet().iterator(); if (it.hasNext()) { return (String) it.next(); } } } catch (IOException e) { throw new RuntimeException("Error reading manifest in jar " + artifact.getFile().getAbsolutePath(), e); } } int i = artifact.getGroupId().lastIndexOf('.'); if ((i < 0) && (artifact.getFile() != null) && artifact.getFile().exists()) { String groupIdFromPackage = getGroupIdFromPackage(artifact.getFile()); if (groupIdFromPackage != null) { return groupIdFromPackage; } } String lastSection = artifact.getGroupId().substring(++i); if (artifact.getArtifactId().equals(lastSection)) { return artifact.getGroupId(); } if (artifact.getArtifactId().startsWith(lastSection)) { String artifactId = artifact.getArtifactId().substring(lastSection.length()); if (Character.isLetterOrDigit(artifactId.charAt(0))) { return getBundleSymbolicName(artifact.getGroupId(), artifactId); } else { return getBundleSymbolicName(artifact.getGroupId(), artifactId.substring(1)); } } return getBundleSymbolicName(artifact.getGroupId(), artifact.getArtifactId()); }
From source file:hudson.ClassicPluginStrategy.java
@Override public String getShortName(File archive) throws IOException { Manifest manifest;/*w w w . j av a2s .co m*/ if (isLinked(archive)) { manifest = loadLinkedManifest(archive); } else { JarFile jf = new JarFile(archive, false); try { manifest = jf.getManifest(); } finally { jf.close(); } } return PluginWrapper.computeShortName(manifest, archive.getName()); }
From source file:net.minecraftforge.fml.common.asm.FMLSanityChecker.java
@Override public Void call() throws Exception { CodeSource codeSource = getClass().getProtectionDomain().getCodeSource(); boolean goodFML = false; boolean fmlIsJar = false; if (codeSource.getLocation().getProtocol().equals("jar")) { fmlIsJar = true;//w ww. j a v a 2s . c o m Certificate[] certificates = codeSource.getCertificates(); if (certificates != null) { for (Certificate cert : certificates) { String fingerprint = CertificateHelper.getFingerprint(cert); if (fingerprint.equals(FMLFINGERPRINT)) { FMLRelaunchLog.info("Found valid fingerprint for FML. Certificate fingerprint %s", fingerprint); goodFML = true; } else if (fingerprint.equals(FORGEFINGERPRINT)) { FMLRelaunchLog.info( "Found valid fingerprint for Minecraft Forge. Certificate fingerprint %s", fingerprint); goodFML = true; } else { FMLRelaunchLog.severe("Found invalid fingerprint for FML: %s", fingerprint); } } } } else { goodFML = true; } // Server is not signed, so assume it's good - a deobf env is dev time so it's good too boolean goodMC = FMLLaunchHandler.side() == Side.SERVER || !liveEnv; int certCount = 0; try { Class<?> cbr = Class.forName("net.minecraft.client.ClientBrandRetriever", false, cl); codeSource = cbr.getProtectionDomain().getCodeSource(); } catch (Exception e) { // Probably a development environment, or the server (the server is not signed) goodMC = true; } JarFile mcJarFile = null; if (fmlIsJar && !goodMC && codeSource.getLocation().getProtocol().equals("jar")) { try { String mcPath = codeSource.getLocation().getPath().substring(5); mcPath = mcPath.substring(0, mcPath.lastIndexOf('!')); mcPath = URLDecoder.decode(mcPath, Charsets.UTF_8.name()); mcJarFile = new JarFile(mcPath, true); mcJarFile.getManifest(); JarEntry cbrEntry = mcJarFile.getJarEntry("net/minecraft/client/ClientBrandRetriever.class"); InputStream mcJarFileInputStream = mcJarFile.getInputStream(cbrEntry); try { ByteStreams.toByteArray(mcJarFileInputStream); } finally { IOUtils.closeQuietly(mcJarFileInputStream); } Certificate[] certificates = cbrEntry.getCertificates(); certCount = certificates != null ? certificates.length : 0; if (certificates != null) { for (Certificate cert : certificates) { String fingerprint = CertificateHelper.getFingerprint(cert); if (fingerprint.equals(MCFINGERPRINT)) { FMLRelaunchLog.info("Found valid fingerprint for Minecraft. Certificate fingerprint %s", fingerprint); goodMC = true; } } } } catch (Throwable e) { FMLRelaunchLog.log(Level.ERROR, e, "A critical error occurred trying to read the minecraft jar file"); } finally { Java6Utils.closeZipQuietly(mcJarFile); } } else { goodMC = true; } if (!goodMC) { FMLRelaunchLog.severe( "The minecraft jar %s appears to be corrupt! There has been CRITICAL TAMPERING WITH MINECRAFT, it is highly unlikely minecraft will work! STOP NOW, get a clean copy and try again!", codeSource.getLocation().getFile()); if (!Boolean.parseBoolean(System.getProperty("fml.ignoreInvalidMinecraftCertificates", "false"))) { FMLRelaunchLog.severe( "For your safety, FML will not launch minecraft. You will need to fetch a clean version of the minecraft jar file"); FMLRelaunchLog.severe( "Technical information: The class net.minecraft.client.ClientBrandRetriever should have been associated with the minecraft jar file, " + "and should have returned us a valid, intact minecraft jar location. This did not work. Either you have modified the minecraft jar file (if so " + "run the forge installer again), or you are using a base editing jar that is changing this class (and likely others too). If you REALLY " + "want to run minecraft in this configuration, add the flag -Dfml.ignoreInvalidMinecraftCertificates=true to the 'JVM settings' in your launcher profile."); FMLCommonHandler.instance().exitJava(1, false); } else { FMLRelaunchLog.severe( "FML has been ordered to ignore the invalid or missing minecraft certificate. This is very likely to cause a problem!"); FMLRelaunchLog.severe( "Technical information: ClientBrandRetriever was at %s, there were %d certificates for it", codeSource.getLocation(), certCount); } } if (!goodFML) { FMLRelaunchLog.severe("FML appears to be missing any signature data. This is not a good thing"); } return null; }
From source file:org.apache.maven.shared.osgi.DefaultMaven2OsgiConverter.java
private String getGroupIdFromPackage(File artifactFile) { try {/*from w ww . j a v a2 s . c o m*/ /* get package names from jar */ Set packageNames = new HashSet(); JarFile jar = new JarFile(artifactFile, false); Enumeration entries = jar.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (entry.getName().endsWith(".class")) { File f = new File(entry.getName()); String packageName = f.getParent(); if (packageName != null) { packageNames.add(packageName); } } } jar.close(); /* find the top package */ String[] groupIdSections = null; for (Iterator it = packageNames.iterator(); it.hasNext();) { String packageName = (String) it.next(); String[] packageNameSections = packageName.split("\\" + FILE_SEPARATOR); if (groupIdSections == null) { /* first candidate */ groupIdSections = packageNameSections; } else // if ( packageNameSections.length < groupIdSections.length ) { /* * find the common portion of current package and previous selected groupId */ int i; for (i = 0; (i < packageNameSections.length) && (i < groupIdSections.length); i++) { if (!packageNameSections[i].equals(groupIdSections[i])) { break; } } groupIdSections = new String[i]; System.arraycopy(packageNameSections, 0, groupIdSections, 0, i); } } if ((groupIdSections == null) || (groupIdSections.length == 0)) { return null; } /* only one section as id doesn't seem enough, so ignore it */ if (groupIdSections.length == 1) { return null; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < groupIdSections.length; i++) { sb.append(groupIdSections[i]); if (i < groupIdSections.length - 1) { sb.append('.'); } } return sb.toString(); } catch (IOException e) { /* we took all the precautions to avoid this */ throw new RuntimeException(e); } }