List of usage examples for java.util.jar JarFile JarFile
public JarFile(File file) throws IOException
From source file:goja.initialize.ctxbox.ClassSearcher.java
/** * jarclass/*from w w w . ja v a 2s . co m*/ */ private List<String> findjarFiles(String baseDirName) { List<String> classFiles = Lists.newArrayList(); File baseDir = new File(baseDirName); if (!baseDir.exists() || !baseDir.isDirectory()) { LOG.error("file search error:" + baseDirName + " is not a dir?"); } else { File[] files = baseDir.listFiles(); if (files == null) { return Collections.EMPTY_LIST; } for (File file : files) { if (file.isDirectory()) { classFiles.addAll(findjarFiles(file.getAbsolutePath())); } else { if (includeAllJarsInLib || includeJars.contains(file.getName())) { JarFile localJarFile = null; try { localJarFile = new JarFile(new File(baseDirName + File.separator + file.getName())); Enumeration<JarEntry> entries = localJarFile.entries(); while (entries.hasMoreElements()) { JarEntry jarEntry = entries.nextElement(); String entryName = jarEntry.getName(); if (scanPackages.isEmpty()) { if (!jarEntry.isDirectory() && entryName.endsWith(".class")) { String className = StringUtils.replace(entryName, StringPool.SLASH, ".") .substring(0, entryName.length() - 6); classFiles.add(className); } } else { for (String scanPackage : scanPackages) { scanPackage = scanPackage.replaceAll("\\.", "\\" + File.separator); if (!jarEntry.isDirectory() && entryName.endsWith(".class") && entryName.startsWith(scanPackage)) { String className = StringUtils.replace(entryName, File.separator, ".") .substring(0, entryName.length() - 6); classFiles.add(className); } } } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (localJarFile != null) { localJarFile.close(); } } catch (IOException e) { LOG.error("close jar file has error!", e); } } } } } } return classFiles; }
From source file:ClassFileUtilities.java
private static void collectJars(File dir, Map jars, Map classFiles) throws IOException { File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { String n = files[i].getName(); if (n.endsWith(".jar") && files[i].isFile()) { Jar j = new Jar(); j.name = files[i].getPath(); j.file = files[i];/*from w w w.j a va2s . c om*/ j.jarFile = new JarFile(files[i]); jars.put(j.name, j); Enumeration entries = j.jarFile.entries(); while (entries.hasMoreElements()) { ZipEntry ze = (ZipEntry) entries.nextElement(); String name = ze.getName(); if (name.endsWith(".class")) { ClassFile cf = new ClassFile(); cf.name = name; cf.jar = j; classFiles.put(j.name + '!' + cf.name, cf); j.files.add(cf); } } } else if (files[i].isDirectory()) { collectJars(files[i], jars, classFiles); } } }
From source file:info.evanchik.eclipse.karaf.core.KarafCorePluginUtils.java
/** * Reads a specified MANIFEST.MF entry from the JAR * * @param src//from ww w . ja v a 2 s .co m * the JAR file * @param manifestHeader * the name of the header to read * @return the header as read from the MANIFEST.MF or null if it does not * exist or there was a problem reading the JAR * @throws IOException * if there is a problem reading the JAR */ public static String getJarManifestHeader(final File src, final String manifestHeader) throws IOException { final JarFile jar = new JarFile(src); final Manifest mf = jar.getManifest(); return (String) mf.getMainAttributes().get(manifestHeader); }
From source file:com.googlecode.onevre.utils.ServerClassLoader.java
/** * * @see java.lang.ClassLoader#findLibrary(java.lang.String) */// w ww . j ava2s. c om protected String findLibrary(String libname) { try { String name = System.mapLibraryName(libname + "-" + System.getProperty("os.arch")); URL url = getResourceURL(name); log.info("Loading " + name + " from " + url); if (url != null) { File jar = cachedJars.get(url); JarFile jarFile = new JarFile(jar); JarEntry entry = jarFile.getJarEntry(name); File library = new File(localLibDirectory, name); if (!library.exists()) { InputStream input = jarFile.getInputStream(entry); FileOutputStream output = new FileOutputStream(library); byte[] buffer = new byte[BUFFER_SIZE]; int totalBytes = 0; while (totalBytes < entry.getSize()) { int bytesRead = input.read(buffer); if (bytesRead < 0) { throw new IOException("Jar Entry too short!"); } output.write(buffer, 0, bytesRead); totalBytes += bytesRead; } output.close(); input.close(); jarFile.close(); } return library.getAbsolutePath(); } } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:com.opengamma.web.WebAbout.java
/** * Gets the OpenGamma version./* w w w . ja v a 2s . co m*/ * @return the version, not null */ public String getOpenGammaBuild() { URL url = ClasspathHelper.forClass(getClass(), getClass().getClassLoader()); if (url != null && url.toString().contains(".jar")) { try { final String part = ClasspathHelper.cleanPath(url); try (JarFile myJar = new JarFile(part)) { final Manifest manifest = myJar.getManifest(); if (manifest != null) { Attributes attributes = manifest.getMainAttributes(); if (attributes != null) { if (attributes.getValue(IMPLEMENTATION_BUILD) != null) { return attributes.getValue(IMPLEMENTATION_BUILD); } } } } } catch (Exception ex) { s_logger.warn(ex.getMessage(), ex); } } return "?"; }
From source file:org.clinigrid.capillary.inspector.Inspector.java
public void run() throws InspectorException { URI outputURI = URI.createFileURI(outputFilename); Resource outputResource = resourceSet.createResource(outputURI); IModelManager modelManager = new DefaultModelManager(outputResource, packagePrefix, uriPrefix); for (String externalModelURIString : externalModelURIs) { URI externalModelURI = URI.createFileURI(externalModelURIString); Resource externalModelResource = resourceSet.createResource(externalModelURI); try {/*from ww w . java2 s.c o m*/ externalModelResource.load(Collections.emptyMap()); } catch (IOException e) { throw new InspectorException("Failed to read external model '" + externalModelURIString + "'", e); } modelManager.importExternalResource(externalModelResource); } DefaultInputCrawler inputCrawler; try { File file = new File(archiveFilePath); if (!file.exists()) { throw new InspectorException("File doesn't exist '" + archiveFilePath + "'"); } if (archiveFilePath.endsWith(".zip") | archiveFilePath.endsWith(".jar") | archiveFilePath.endsWith(".war")) { JarFile jarFile = new JarFile(file); inputCrawler = new DefaultInputCrawler(jarFile); } else { throw new InspectorException("Unknown archive file format '" + archiveFilePath + "'"); } } catch (IOException e) { throw new InspectorException("Failed to read JAR file '" + archiveFilePath + "'", e); } System.out.printf("[Inspector] Loaded archive '%s'\n", archiveFilePath); System.out.flush(); for (String dependencyPath : dependencyPaths) { try { File file = new File(dependencyPath); if (!file.exists()) { throw new InspectorException("File doesn't exist '" + dependencyPath + "'"); } JarFile jarFile = new JarFile(file); inputCrawler.addDependency(jarFile); } catch (IOException e) { throw new InspectorException("Failed to read JAR file '" + dependencyPath + "'", e); } } DefaultContributionReporter reporter = new DefaultContributionReporter(); reporter.setVerbose(verbose); for (IModelContributor modelContributor : modelContributors) { reporter.beginModelContributor(modelContributor); try { modelContributor.contributeToModel(modelManager, inputCrawler, reporter); } catch (ContributorException e) { throw new InspectorException("Contributor failed '" + modelContributor.getName() + "'", e); } catch (RuntimeException e) { throw new InspectorException("Contributor failed '" + modelContributor.getName() + "'", e); } reporter.endModelContributor(modelContributor); } ((DefaultModelManager) modelManager).truncateRoot(uriPrefix); try { outputResource.save(null); } catch (IOException e) { throw new InspectorException("Failed to save output file", e); } }
From source file:javadepchecker.Main.java
/** * Core method, this one fires off all others and is the one called from * Main. Check this package for unneeded dependencies and orphaned class * files// www .ja v a2 s . co m * * @param env * @return */ private static boolean checkPkg(File env) { boolean needed = true; boolean found = true; HashSet<String> pkgs = new HashSet<>(); Collection<String> deps = null; InputStream is = null; try { // load package.env Properties props = new Properties(); is = new FileInputStream(env); props.load(is); // load package deps, add to hashset if exist String depend = props.getProperty("DEPEND"); if (depend != null && !depend.isEmpty()) { for (String atom : depend.replaceAll("\"", "").split(":")) { String pkg = atom; if (atom.contains("@")) { pkg = atom.split("@")[1]; } pkgs.add(pkg); } } // load package classpath String classpath = props.getProperty("CLASSPATH"); if (classpath != null && !classpath.isEmpty()) { Main classParser = new Main(); for (String jar : classpath.replaceAll("\"", "").split(":")) { if (jar.endsWith(".jar")) { classParser.processJar(new JarFile(image + jar)); } } deps = classParser.getDeps(); } for (String pkg : pkgs) { if (!depNeeded(pkg, deps)) { if (needed) { System.out.println("Possibly unneeded dependencies found"); } System.out.println("\t" + pkg); needed = false; } } found = depsFound(pkgs, deps); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } finally { try { if (is != null) is.close(); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } return needed && found; }
From source file:com.redhat.ceylon.compiler.java.test.cmr.CMRTests.java
@Test public void testMdlModuleDefaultJavaFile() throws IOException { compile("modules/def/JavaClass.java"); File carFile = getModuleArchive("default", null); assertTrue(carFile.exists());//w w w. j a v a 2 s .c o m JarFile car = new JarFile(carFile); ZipEntry moduleClass = car.getEntry("com/redhat/ceylon/compiler/java/test/cmr/modules/def/JavaClass.class"); assertNotNull(moduleClass); car.close(); }
From source file:net.minecraftforge.fml.relauncher.libraries.LibraryManager.java
private static Pair<Artifact, byte[]> extractPacked(File file, ModList modlist, File... modDirs) { if (processed.contains(file)) { FMLLog.log.debug("File already proccessed {}, Skipping", file.getAbsolutePath()); return null; }//from w ww . jav a 2 s. c o m JarFile jar = null; try { jar = new JarFile(file); FMLLog.log.debug("Examining file: {}", file.getName()); processed.add(file); return extractPacked(jar, modlist, modDirs); } catch (IOException ioe) { FMLLog.log.error("Unable to read the jar file {} - ignoring", file.getName(), ioe); } finally { try { if (jar != null) jar.close(); } catch (IOException e) { } } return null; }
From source file:org.jtheque.modules.impl.ModuleLoader.java
/** * Read the config of the module./*ww w .jav a 2 s . co m*/ * * @param file The file of the module. * * @return The module resources. * * @throws IOException If an error occurs during Jar File reading. * @throws org.jtheque.modules.ModuleException * If the config cannot be read. */ private ModuleResources readConfig(File file) throws IOException, ModuleException { JarFile jarFile = null; try { jarFile = new JarFile(file); ZipEntry configEntry = jarFile.getEntry("module.xml"); if (configEntry == null) { return new ModuleResources(CollectionUtils.<ImageResource>emptyList(), CollectionUtils.<I18NResource>emptyList(), CollectionUtils.<Resource>emptyList()); } ModuleResources resources = importConfig(jarFile.getInputStream(configEntry)); //Install necessary resources before installing the bundle for (Resource resource : resources.getResources()) { if (resource != null) { resourceService.installResource(resource); } } return resources; } finally { if (jarFile != null) { jarFile.close(); } } }