use of java.util.jar.JarFile in project CoreNLP by stanfordnlp.
the class ArgumentParser method getVisibleClasses.
private static Class<?>[] getVisibleClasses() {
//--Variables
List<Class<?>> classes = new ArrayList<>();
// (get classpath)
String pathSep = System.getProperty("path.separator");
String[] cp = System.getProperties().getProperty("java.class.path", null).split(pathSep);
// (get classes)
for (String entry : cp) {
log("Checking cp " + entry);
//(should skip?)
if (entry.equals(".") || entry.trim().length() == 0) {
continue;
}
//(no, don't skip)
File f = new File(entry);
if (f.isDirectory()) {
// --Case: Files
LazyFileIterator iter = new LazyFileIterator(f, ".*class$");
while (iter.hasNext()) {
//(get the associated class)
Class<?> clazz = filePathToClass(entry, iter.next().getPath());
if (clazz != null) {
//(add the class if it's valid)
classes.add(clazz);
}
}
} else //noinspection StatementWithEmptyBody
if (!isIgnored(entry)) {
// --Case: Jar
try {
JarFile jar = new JarFile(f);
Enumeration<JarEntry> e = jar.entries();
while (e.hasMoreElements()) {
//(for each jar file element)
JarEntry jarEntry = e.nextElement();
String clazz = jarEntry.getName();
if (clazz.matches(".*class$")) {
//(if it's a class)
clazz = clazz.substring(0, clazz.length() - 6).replaceAll("/", ".");
//(add it)
try {
classes.add(Class.forName(clazz, false, ClassLoader.getSystemClassLoader()));
} catch (ClassNotFoundException ex) {
warn("Could not load class in jar: " + f + " at path: " + clazz);
} catch (NoClassDefFoundError ex) {
debug("Could not scan class: " + clazz + " (in jar: " + f + ")");
}
}
}
} catch (IOException e) {
warn("Could not open jar file: " + f + "(are you sure the file exists?)");
}
} else {
//case: ignored jar
}
}
return classes.toArray(new Class<?>[classes.size()]);
}
use of java.util.jar.JarFile in project springside4 by springside.
the class ResourceUtilTest method resourceNameTest.
@Test
public void resourceNameTest() throws IOException {
JarFile guavaFile = new JarFile(FilePathUtil.getJarPath(Files.class));
assertThat(guavaFile.getEntry("META-INF/MANIFEST.MF")).isNotNull();
assertThat(guavaFile.getEntry("/META-INF/MANIFEST.MF")).isNull();
guavaFile.close();
}
use of java.util.jar.JarFile in project jetbrick-template-1x by subchen.
the class JarResource method getSource.
@Override
public char[] getSource(String encoding) {
JarFile jarFile = null;
try {
jarFile = new JarFile(jar);
JarEntry jarEntry = jarFile.getJarEntry(entry);
if (jarEntry == null)
return null;
InputStream is = jarFile.getInputStream(jarEntry);
try {
return IoUtils.toCharArray(is, encoding);
} finally {
IoUtils.closeQuietly(is);
}
} catch (IOException e) {
throw ExceptionUtils.uncheck(e);
} finally {
IoUtils.closeQuietly(jarFile);
}
}
use of java.util.jar.JarFile in project skype-bot by toomasr.
the class PluginScanner method scan.
public Collection<BotPlugin> scan(File file) {
List<BotPlugin> plugins = new ArrayList<BotPlugin>();
try (JarFile jar = new JarFile(file)) {
URLClassLoader classloader = new URLClassLoader(new URL[] { file.toURI().toURL() });
Plugins.registerPluginClassloader(file, classloader);
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (entry.isDirectory() || !entry.getName().startsWith("org/zeroturnaround/skypebot") || !entry.getName().endsWith(".class")) {
continue;
}
// we loaded all the classes from this jar file. we can close the classloader
Class<?> clazz = classloader.loadClass(entry.getName().replace('/', '.').replace(".class", ""));
if (clazz.getAnnotation(SkypeBotPlugin.class) != null) {
Object pluginInstance = clazz.newInstance();
if (BotPlugin.class.isInstance(pluginInstance)) {
log.info("Found a plugin: {}", clazz.getSimpleName());
plugins.add(BotPlugin.class.cast(pluginInstance));
}
}
}
return plugins;
} catch (Exception e) {
log.error("Failed to scan file " + file + " for plugin", e);
return Collections.emptyList();
}
}
use of java.util.jar.JarFile in project bazel by bazelbuild.
the class IjarTests method readJar.
static Map<String, byte[]> readJar(String path) throws IOException {
Map<String, byte[]> classes = new HashMap<>();
try (JarFile jf = new JarFile(path)) {
Enumeration<JarEntry> entries = jf.entries();
while (entries.hasMoreElements()) {
JarEntry je = entries.nextElement();
if (!je.getName().endsWith(".class")) {
continue;
}
classes.put(je.getName(), ByteStreams.toByteArray(jf.getInputStream(je)));
}
}
return classes;
}
Aggregations