[英]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異常。有人可以讓我知道我們應該如何將jar路徑提供給JarFile構造函數?
試試這個: JarFile jf = new JarFile("lib/xxxx-1.0.0.jar");
感謝用戶@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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.