从eclipse插件中的jar文件中读取类名(read class names from a jar file in eclipse plug-in)
我需要从jar文件(OSGified)中读取类名(只是简单名称)。 我已将jar文件放在lib文件夹中,并将其添加到类路径中。 这是我写的代码:
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); } } }不知何故,我在构造jarfile对象时遇到了Filenotfound异常。有人能告诉我们应该如何给JarFile构造函数提供jar路径吗?
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); } }更多推荐
发布评论