简体   繁体   中英

read class names from a jar file in eclipse plug-in

I need to read class names(just the simple name) from a jar file(OSGified). I've placed the jar file in the lib folder and it's added to class path. Here is the code I've written:

public void  loadClassName() throws IOException {
    JarFile jf = new JarFile("/lib/xxxx-1.0.0.jar");
    List<String> list = new ArrayList<String>();
    for (JarEntry entry : Collections.list(jf.entries())) {
        if (entry.getName().endsWith(".class")) {
            String className = entry.getName().replace("/", ".").replace(".class", "");
            list.add(className);
        }
    }
}

Somehow, I"m getting Filenotfound exception while constructing the jarfile object. Can somebody let me know how we should give the jar path to the JarFile constructor ?

试试这个: JarFile jf = new JarFile("lib/xxxx-1.0.0.jar");

Thanks to the user @Perception. His answer has worked flawlessly.

This is the working code:

 final InputStream jarStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("lib/xxxxx-1.0.0.jar");
        JarInputStream jfs = new JarInputStream(jarStream);
        List<String> list = new ArrayList<String>();
        JarEntry je = null;
        while (true) {
            je = jfs.getNextJarEntry();
            if (je == null) {
                break;
            }
            if (je.getName().endsWith(".class")) {
                String className = je.getName().replace("/", ".").replace(".class", "");
                    list.add(className);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM