列出 jar 文件中的类

发布于 2024-09-13 15:49:17 字数 28 浏览 9 评论 0原文

如何动态加载 jar 文件并列出其中的类?

How can I dynamically load a jar file and list classes which is in it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ぶ宁プ宁ぶ 2024-09-20 15:49:17

以下是列出 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);
            }
        }
    }
}

Here is code for listing classes in 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);
            }
        }
    }
}
城歌 2024-09-20 15:49:17

您可以使用以下命令从命令提示符查看 JAR 文件的内容:

jar tf jar-file

例如:

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  

You can view the contents of a JAR file from command prompt by using the following command:

jar tf jar-file

For example:

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  
流星番茄 2024-09-20 15:49:17

查看 java.util.jar 包中的类。您可以在 Web 上找到如何列出 JAR 内的文件的示例,这里是 示例。 (另请注意该页面底部的链接,还有更多示例向您展示如何使用 JAR 文件)。

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).

花伊自在美 2024-09-20 15:49:17

快速方法:只需在 7-Zip 中将 .jar 作为 .zip 打开,然后查找目录名称。

Fast way: just open the .jar as .zip e.g. in 7-Zip and look for the directory-names.

遮了一弯 2024-09-20 15:49:17

这是一个扫描给定 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();
}

当您知道 jar 在哪里时,可以直接使用该代码。要获取该信息,请参阅另一个问题,因为通过类路径有自 Java 9 和模块的引入以来发生了变化。

Here is a version that scans a given jar for all non-abstract classes extending a particular class:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文