Java 列出 jar 文件中的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3429275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Listing classes in a jar file
提问by Hossein Margani
How can I dynamically load a jar file and list classes which is in it?
如何动态加载 jar 文件并列出其中的类?
采纳答案by Jesper
Have a look at the classes in the package java.util.jar
. You can find examples of how to list the files inside the JAR on the web, here's an example. (Also note the links at the bottom of that page, there are many more examples that show you how to work with JAR files).
查看包中的类java.util.jar
。您可以在网上找到有关如何列出 JAR 中文件的示例,这是一个示例。(另请注意该页面底部的链接,还有更多示例向您展示如何使用 JAR 文件)。
回答by YoK
Here is code for listing classes in jar:
这是在 jar 中列出类的代码:
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class JarList {
public static void main(String args[]) throws IOException {
if (args.length > 0) {
JarFile jarFile = new JarFile(args[0]);
Enumeration allEntries = jarFile.entries();
while (allEntries.hasMoreElements()) {
JarEntry entry = (JarEntry) allEntries.nextElement();
String name = entry.getName();
System.out.println(name);
}
}
}
}
回答by lama12345
Fast way: just open the .jar as .zip e.g. in 7-Zip and look for the directory-names.
快速方法:只需将 .jar 作为 .zip 打开,例如在 7-Zip 中,然后查找目录名称。
回答by Vishnudev K
Viewing the Contents of a JAR File
查看 JAR 文件的内容
For viewing the contents of a JAR file from command prompt use
要从命令提示符查看 JAR 文件的内容,请使用
jar tf jar-file
jar tf jar 文件
eg:-
例如:-
jar tf TicTacToe.jar
META-INF/MANIFEST.MF
TicTacToe.class
audio/
audio/beep.au
audio/ding.au
audio/return.au
audio/yahoo1.au
audio/yahoo2.au
images/
images/cross.gif
images/not.gif
回答by Matthieu
Here is a version that scans a given jar for all non-abstractclasses extending a particular class:
这是一个扫描给定 jar 以获取扩展特定类的所有非抽象类的版本:
try (JarFile jf = new JarFile("/path/to/file.jar")) {
for (Enumeration<JarEntry> en = jf.entries(); en.hasMoreElements(); ) {
JarEntry e = en.nextElement();
String name = e.getName();
// Check for package or sub-package (you can change the test for *exact* package here)
if (name.startsWith("my/specific/package/") && name.endsWith(".class")) {
// Strip out ".class" and reformat path to package name
String javaName = name.substring(0, name.lastIndexOf('.')).replace('/', '.');
System.out.print("Checking "+javaName+" ... ");
Class<?> cls;
try {
cls = Class.forName(javaName);
} catch (ClassNotFoundException ex) { // E.g. internal classes, ...
continue;
}
if ((cls.getModifiers() & Modifier.ABSTRACT) != 0) { // Only instanciable classes
System.out.println("(abstract)");
continue;
}
if (!TheSuper.class.isAssignableFrom(cls)) { // Only subclasses of "TheSuper" class
System.out.println("(not TheSuper)");
continue;
}
// Found!
System.out.println("OK");
}
}
} catch (IOException e) {
e.printStackTrace();
}
You can use that code directly when you know where are your jars. To get that information, refer to this other question, as going through classpath has changed since Java 9 and the introduction of modules.
当您知道罐子在哪里时,您可以直接使用该代码。要获取该信息,请参阅其他问题,因为自 Java 9 和模块引入以来,通过类路径已发生变化。