@Override protected void scanJarFile(ClassLoader classloader, JarFile file) { Enumeration<JarEntry> entries = file.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.isDirectory() || entry.getName().equals(JarFile.MANIFEST_NAME)) { continue; } resources.get(classloader).add(entry.getName()); } }
protected void addJarEntryToIndex(JarEntry entry, StringBuilder buffer) { // no need to show empty directories if (!entry.isDirectory()) { buffer.append(entry.getName()); buffer.append("\n"); } }
public static List<File> extractFilesInLibDirAndReturnFiles(File aJarFile, Predicate<JarEntry> extractFilter, File outputTmpDir) { List<File> newClassPath = new ArrayList<>(); try (JarFile jarFile = new JarFile(aJarFile)) { List<File> extractedJars = jarFile.stream() .filter(extractFilter) .map(jarEntry -> { String jarFileBaseName = FilenameUtils.getName(jarEntry.getName()); File targetFile = new File(outputTmpDir, jarFileBaseName); return extractJarEntry(jarFile, jarEntry, targetFile); }) .collect(Collectors.toList()); // add deps in dir specified by `libDirManifestKey` newClassPath.addAll(extractedJars); } catch (IOException e) { throw new RuntimeException(e); } return newClassPath; }
JarClassPath(String pathname) throws NotFoundException { JarFile jarfile = null; try { jarfile = new JarFile(pathname); jarfileEntries = new ArrayList<String>(); for (JarEntry je:Collections.list(jarfile.entries())) if (je.getName().endsWith(".class")) jarfileEntries.add(je.getName()); jarfileURL = new File(pathname).getCanonicalFile() .toURI().toURL().toString(); return; } catch (IOException e) {} finally { if (null != jarfile) try { jarfile.close(); } catch (IOException e) {} } throw new NotFoundException(pathname); }
/** * 扫描jar包 * * @param jar jar包 */ private void scanJar(JarFile jar) { String name; for (JarEntry entry : new EnumerationIter<>(jar.entries())) { name = StrUtil.removePrefix(entry.getName(), StrUtil.SLASH); if (name.startsWith(this.packagePath)) { if (name.endsWith(FileUtil.CLASS_EXT) && false == entry.isDirectory()) { final String className = name// .substring(0, name.length() - 6)// .replace(CharUtil.SLASH, CharUtil.DOT);// addIfAccept(loadClass(className)); } } } }
@Override protected void scanJarFile(ClassLoader classloader, JarFile file) { Enumeration<JarEntry> entries = file.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.isDirectory() || entry.getName().equals(JarFile.MANIFEST_NAME)) { continue; } resources.get(classloader).add(entry.getName()); } }
JarClassPath(String pathname) throws NotFoundException { JarFile jarfile = null; try { jarfile = new JarFile(pathname); jarfileEntries = new ArrayList<String>(); for (JarEntry je:Collections.list(jarfile.entries())) if (je.getName().endsWith(".class")) jarfileEntries.add(je.getName()); jarfileURL = new File(pathname).getCanonicalFile() .toURI().toURL().toString(); return; } catch (IOException e) {} finally { if (null != jarfile) try { jarfile.close(); } catch (IOException e) {} } throw new NotFoundException(pathname); }
private void explodeNatives() throws IOException { URL urlOfJar = getJarURL(); LOGGER.info("[TFS SDK] Exploding natives from {} to folder {}", urlOfJar.toString(), tempFolder.getAbsolutePath()); JarInputStream jarStream = new JarInputStream(urlOfJar.openStream()); JarEntry entry; while ((entry = jarStream.getNextJarEntry()) != null) { if (!entry.isDirectory() && entry.getName().startsWith("tfssdk/native/")) { File newFile = new File(tempFolder, entry.getName()); newFile.getParentFile().mkdirs(); LOGGER.info("[TFS SDK] Extract {} -> {}", entry.getName(), newFile); try (OutputStream fos = new FileOutputStream(newFile)) { IOUtils.copy(jarStream, fos); } } } }
/** * 扫描jar包 * * @param jar jar包 */ private void scanJar(JarFile jar) { String name; for (JarEntry entry : new EnumerationIter<>(jar.entries())) { name = StrUtil.removePrefix(entry.getName(), StrUtil.SLASH); if (name.startsWith(this.packagePath)) { if (name.endsWith(FileUtil.CLASS_EXT) && false == entry.isDirectory()) { final String className = name// .substring(0, name.length() - 6)// .replace(CharUtil.SLASH, CharUtil.DOT);// addIfAccept(loadClass(className)); } } } }
private void run() { Handler.init(); try (JarFile jarFile = new JarFile(currentJarFile())) { String mainClassName = mainClassName(jarFile); List<URL> jarsInJar = jarFile.stream() .filter(FIND_JAR_FILES_UNDER_LIB_FOLDER) .map(jarEntry -> Handler.toOneJarUrl(jarEntry.getName())) .collect(Collectors.toList()); List<URL> urls = new ArrayList<>(); urls.addAll(additionalJarsInLibsDirectoryInCWD()); urls.addAll(jarsInJar); ClassLoader jcl = new URLClassLoader(urls.toArray(new URL[0]), getClass().getClassLoader()); Thread.currentThread().setContextClassLoader(jcl); Class<?> mainClass = jcl.loadClass(mainClassName); mainClass.getMethod("main", String[].class).invoke(null, new Object[]{args}); } catch (IOException | ReflectiveOperationException e) { e.printStackTrace(); System.exit(1); } }
@Override protected void scanJarFile(ClassLoader classloader, JarFile file) { Enumeration<JarEntry> entries = file.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.isDirectory() || entry.getName().equals(JarFile.MANIFEST_NAME)) { continue; } resources.get(classloader).add(entry.getName()); } }
private static void extract(JarFile jar, JarEntry file) throws IOException { Path workingDirectory = Paths.get("").toAbsolutePath(); File target = new File(workingDirectory.toString() + File.separator + file.getName()); if (target.exists()) return; if (file.isDirectory()) { target.mkdir(); return; } Files.copy(jar.getInputStream(file), target.toPath()); }
@Override protected void scanJarFile(ClassLoader classloader, JarFile file) { Enumeration<JarEntry> entries = file.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.isDirectory() || entry.getName().equals(JarFile.MANIFEST_NAME)) { continue; } resources.get(classloader).add(entry.getName()); } }
private URL[] enumerateJar(URL urlOfJar) { LOGGER.debug("Enumerating jar: {}", urlOfJar); List<URL> urls = new ArrayList<>(); urls.add(urlOfJar); try { JarInputStream jarStream = new JarInputStream(urlOfJar.openStream()); JarEntry entry; while ((entry = jarStream.getNextJarEntry()) != null) { if (!entry.isDirectory() && entry.getName().endsWith(".jar")) { urls.add(expandJarAndReturnURL(jarStream, entry)); } } } catch (IOException e) { LOGGER.error("Failed to enumerate jar {}", urlOfJar, e); } return urls.toArray(new URL[0]); }
private static Map<String, SQLCase> loadSQLCasesFromJar(final String path, final File file) throws IOException, JAXBException { Map<String, SQLCase> result = new TreeMap<>(); try (JarFile jar = new JarFile(file)) { Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { String name = entries.nextElement().getName(); if (name.startsWith(path + "/") && name.endsWith(".xml")) { fillSQLMap(result, SQLCasesLoader.class.getClassLoader().getResourceAsStream(name)); } } } return result; }
@SuppressWarnings("ResultOfMethodCallIgnored") private void copyFromJarToLocation(final JarFile jarFile, final JarEntry entry, final String fromRoot, final File toRoot) throws IOException { String destinationName = entry.getName().replaceFirst(fromRoot, ""); File newFile = new File(toRoot, destinationName); log.debug("Copying resource {} from JAR file {}", fromRoot, jarFile.getName()); if (!entry.isDirectory()) { // Create parent directories Path parent = newFile.getAbsoluteFile().toPath().getParent(); parent.toFile().mkdirs(); newFile.deleteOnExit(); try (InputStream is = jarFile.getInputStream(entry)) { Files.copy(is, newFile.toPath()); } catch (IOException e) { log.error("Failed to extract classpath resource " + entry.getName() + " from JAR file " + jarFile.getName(), e); throw e; } } }
private void loadJarContent(JarURLConnection url, String packageName, Set<InputStream> streams) throws IOException { // Using a JarURLConnection will load the JAR from the cache when using Webstart 1.6 // In Webstart 1.5, the URL will point to the cached JAR on the local filesystem JarFile jarFile = url.getJarFile(); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.getName().startsWith(packageName)) { streams.add(jarFile.getInputStream(entry)); } } }
@Override public boolean hasNext() { if (next == null) { try { do { this.next = jarInputStream.getNextJarEntry(); if (next == null) { break; } if (!next.isDirectory() && next.getName().startsWith(parent)) { if (recursive || next.getName().substring(parent.length()).indexOf(JAR_FILE_SEPARATOR) == -1) { break; } } } while (true); } catch (final IOException | SecurityException e) { LOGGER.log(Level.CONFIG, LocalizationMessages.JAR_SCANNER_UNABLE_TO_READ_ENTRY(), e); return false; } } if (next == null) { close(); return false; } return true; }
public List<String> getAllEntriesFromJar() { List<String> mass = new ArrayList<>(); Enumeration<JarEntry> entries = jfile.entries(); while (entries.hasMoreElements()) { JarEntry e = entries.nextElement(); if (!e.isDirectory()) { mass.add(e.getName()); } } return mass; }