java - 遍历几个 jar

标签 java eclipse jar

我刚刚编写了一个程序,它遍历一个 jar 文件,并打印出每个 .class 文件的所有合格路径名

我的代码如下所示:

public static ArrayList<String> getClassNames(String jarName) {
    ArrayList<String> CorbaClasses = new ArrayList<String>();

        System.out.println("Jar " + jarName );
    try {

        JarInputStream jarFile = new JarInputStream(new FileInputStream(
                jarName));
        JarEntry jarEntry;

        while (true) {
            jarEntry = jarFile.getNextJarEntry();
            if (jarEntry == null) {
                break;
            }
            if (jarEntry.getName().endsWith(".class")) {

                    System.out.println("Found "
                            + jarEntry.getName().replaceAll("/", "\\.").replace(".class", ""));
                CorbaClasses.add(jarEntry.getName().replaceAll("/", "\\."));
                jarFile.close();
            }
        }
        jarFile.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return CorbaClasses;

}

我现在需要的不是只遍历一个 jar 文件,而是遍历一堆文件夹,每个文件夹中都有一个 jar 文件,我需要打印出类文件的路径名。

我该怎么做?

最佳答案

要遍历多个目录/子目录,请使用以下代码。

File path = new File("your root Directory path");
File [] files = path.listFiles();//return list of all folder plus file in root directory
    for (int i = 0; i < files.length; i++){
        if (files[i].isFile()){ //if it is file do something
            System.out.println(files[i]);
        }else {
            //it is directory go inside to find jar files
            //if inside also directory found make it function and call it recursively
        }
    }

关于java - 遍历几个 jar ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29773597/

相关文章:

java - 检查是否存在具有相同值的行和列的有效算法?

android - 无法从 Eclipse 中的 xml 布局文件启动 android 项目

java - Eclipse 工作区已损坏

java - 如何让maven为每个版本的程序使用单独的目标文件夹?

java - 从 jar 中读取文本文件

java - JSF - selectItems 不会在列表内呈现列表

java - 对于 IntelliJ 中的 Java 项目,Run 命令究竟做了什么?从 ./mvnw 命令获取不同的运行时行为

Spring Boot jar 由于缺少 ServletWebServerFactory bean 而无法启动 Web 服务器

java - 如何从可执行 jar 中的 main() 运行 TestNG 测试?

java - 创建未知数量的 Java 节点