从eclipse插件中的jar文件中读取类名(read class names from a jar file in eclipse plug

编程入门 行业动态 更新时间:2024-10-06 20:27:42
从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); } }

更多推荐

本文发布于:2023-08-07 17:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465218.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:插件   文件   jar   eclipse   read

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!