Java 从eclipse插件中的jar文件读取类名
我需要从jar文件(OSGified)中读取类名(只是简单的名称)。我已将jar文件放在lib文件夹中,并将其添加到类路径中。以下是我编写的代码:Java 从eclipse插件中的jar文件读取类名,java,eclipse-plugin,Java,Eclipse Plugin,我需要从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 (Jar
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);
}
}
}
public void loadClassName()引发IOException{
JarFile jf=newjarfile(“/lib/xxxx-1.0.0.jar”);
列表=新的ArrayList();
for(JarEntry:Collections.list(jf.entries())){
if(entry.getName().endsWith(“.class”)){
字符串className=entry.getName().replace(“/”,“).replace(“.class”,”);
list.add(className);
}
}
}
不知怎的,我在构造jarfile对象时遇到了Filenotfound异常。有人能告诉我应该如何为jarfile构造函数提供jar路径吗?试试以下方法:
JarFile jf=new-JarFile(“lib/xxxx-1.0.0.jar”);
多亏了user@Perception。他的答案完美无瑕
这是工作代码:
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);
}
}
final InputStream jarStream=Thread.currentThread().getContextClassLoader().getResourceAsStream(“lib/xxxxx-1.0.0.jar”);
JarInputStream jfs=新的JarInputStream(jarStream);
列表=新的ArrayList();
jarje=null;
while(true){
je=jfs.getNextJarEntry();
如果(je==null){
打破
}
if(je.getName().endsWith(“.class”)){
字符串className=je.getName().replace(“/”,“).replace(“.class”,”);
list.add(className);
}
}
如果您解释了为什么要提出此建议,您的答案会有很大改进。