簡體   English   中英

獲取類路徑中的所有類

[英]Get all of the Classes in the Classpath

如何在運行時獲取CLASSPATH中所有可用類的列表?
在 Eclipse IDE 中,您可以通過按Ctrl + Shift + T來執行此操作。
Java中有什么方法可以完成它嗎?

您可以通過將空String傳遞給ClassLoader#getResources()來獲取所有類路徑根。

Enumeration<URL> roots = classLoader.getResources("");

您可以根據URL構造一個File ,如下所示:

File root = new File(url.getPath());

您可以使用File#listFiles()獲取給定目錄中所有文件的列表:

for (File file : root.listFiles()) {
    // ...
}

您可以使用標准的java.io.File方法來檢查它是否是目錄和/或獲取文件名。

if (file.isDirectory()) {
    // Loop through its listFiles() recursively.
} else {
    String name = file.getName();
    // Check if it's a .class file or a .jar file and handle accordingly.
}

根據唯一的功能需求,我想Reflections 庫更符合您的需求。

這是我寫的內容。 我敢肯定,如果您對類路徑做了任何奇怪的事情,它不會得到一切,但它似乎對我來說效果很好。 請注意,它實際上並不加載類,它只是返回它們的名稱。 這是為了它不會將所有類加載到內存中,並且因為如果在錯誤的時間加載,我公司代碼庫中的某些類會導致初始化錯誤......

public interface Visitor<T> {
    /**
     * @return {@code true} if the algorithm should visit more results,
     * {@code false} if it should terminate now.
     */
    public boolean visit(T t);
}

public class ClassFinder {
    public static void findClasses(Visitor<String> visitor) {
        String classpath = System.getProperty("java.class.path");
        String[] paths = classpath.split(System.getProperty("path.separator"));

        String javaHome = System.getProperty("java.home");
        File file = new File(javaHome + File.separator + "lib");
        if (file.exists()) {
            findClasses(file, file, true, visitor);
        }

        for (String path : paths) {
            file = new File(path);
            if (file.exists()) {
                findClasses(file, file, false, visitor);
            }
        }
    }

    private static boolean findClasses(File root, File file, boolean includeJars, Visitor<String> visitor) {
        if (file.isDirectory()) {
            for (File child : file.listFiles()) {
                if (!findClasses(root, child, includeJars, visitor)) {
                    return false;
                }
            }
        } else {
            if (file.getName().toLowerCase().endsWith(".jar") && includeJars) {
                JarFile jar = null;
                try {
                    jar = new JarFile(file);
                } catch (Exception ex) {

                }
                if (jar != null) {
                    Enumeration<JarEntry> entries = jar.entries();
                    while (entries.hasMoreElements()) {
                        JarEntry entry = entries.nextElement();
                        String name = entry.getName();
                        int extIndex = name.lastIndexOf(".class");
                        if (extIndex > 0) {
                            if (!visitor.visit(name.substring(0, extIndex).replace("/", "."))) {
                                return false;
                            }
                        }
                    }
                }
            }
            else if (file.getName().toLowerCase().endsWith(".class")) {
                if (!visitor.visit(createClassName(root, file))) {
                    return false;
                }
            }
        }

        return true;
    }

    private static String createClassName(File root, File file) {
        StringBuffer sb = new StringBuffer();
        String fileName = file.getName();
        sb.append(fileName.substring(0, fileName.lastIndexOf(".class")));
        file = file.getParentFile();
        while (file != null && !file.equals(root)) {
            sb.insert(0, '.').insert(0, file.getName());
            file = file.getParentFile();
        }
        return sb.toString();
    }
}

要使用它:

ClassFinder.findClasses(new Visitor<String>() {
    @Override
    public boolean visit(String clazz) {
        System.out.println(clazz)
        return true; // return false if you don't want to see any more classes
    }
});

您可以使用來自 Guava 庫的com.google.common.reflect包中的實用程序類。 例如,獲取特定包中的所有類:

    ClassLoader cl = getClass().getClassLoader();
    Set<ClassPath.ClassInfo> classesInPackage = ClassPath.from(cl).getTopLevelClassesRecursive("com.mycompany.mypackage");

這很簡潔,但是與其他答案描述的相同警告仍然適用,即它通常只會找到由“標准”類加載器加載的類,例如URLClassLoader

我經常找這個。 這有點困難,因為即使您設法在類路徑上找到所有內容,您也可能無法找到給定類加載器可用的所有內容(例如,我曾參與過一個直接從 DB 加載類定義的項目)。

此時最好的選擇可能是研究 Spring。 他們掃描類路徑上的類以查看是否有任何需要啟動的注釋。

這里接受的答案是一個很好的起點:

在運行時掃描 Java 注釋

我建議使用Spring的PathMatchingResourcePatternResolver ;

無論是從IDE還是從文件系統啟動包,它都可以解決問題:

有關詳細信息,請查看我的注釋:

如何從包中獲取資源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM