Search in sources :

Example 96 with JarFile

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()]);
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 97 with JarFile

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();
}
Also used : JarFile(java.util.jar.JarFile) Files(com.google.common.io.Files) Test(org.junit.Test)

Example 98 with JarFile

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);
    }
}
Also used : JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 99 with 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();
    }
}
Also used : SkypeBotPlugin(org.zeroturnaround.skypebot.plugins.SkypeBotPlugin) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) SkypeBotPlugin(org.zeroturnaround.skypebot.plugins.SkypeBotPlugin) BotPlugin(org.zeroturnaround.skypebot.plugins.BotPlugin) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 100 with JarFile

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;
}
Also used : HashMap(java.util.HashMap) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Aggregations

JarFile (java.util.jar.JarFile)509 File (java.io.File)289 JarEntry (java.util.jar.JarEntry)230 IOException (java.io.IOException)212 URL (java.net.URL)92 ZipEntry (java.util.zip.ZipEntry)92 InputStream (java.io.InputStream)90 Manifest (java.util.jar.Manifest)81 ZipFile (java.util.zip.ZipFile)74 FileOutputStream (java.io.FileOutputStream)71 Test (org.junit.Test)66 ArrayList (java.util.ArrayList)54 JarURLConnection (java.net.JarURLConnection)48 Attributes (java.util.jar.Attributes)43 JarOutputStream (java.util.jar.JarOutputStream)41 MalformedURLException (java.net.MalformedURLException)31 FileInputStream (java.io.FileInputStream)30 Support_PlatformFile (tests.support.Support_PlatformFile)26 Enumeration (java.util.Enumeration)24 HashSet (java.util.HashSet)20