Java - iterate all classpath with spring framework

less than 1 minute read

The following code will iterate all available classpath in the application.

1
2
3
4
5
6
7
8
try {
    Enumeration<URL> resources = ApplicationContextProvider.getApplicationContext().getClassLoader().getResources("");
    while (resources.hasMoreElements()) {
        URL url = resources.nextElement();
        String path = url.getPath();
    }
} catch (Exception e) {
}

Iterate classpath for the jar application

1
2
3
4
5
6
7
8
9
10
11
12
    private static void iterateClassPath() {
        try {
            Enumeration<URL> resources = MainApp.class.getClassLoader().getResources("");
            while (resources.hasMoreElements()) {
                URL url = resources.nextElement();
                String path = url.getPath();
                System.out.println("Classpath: " + path);
            }
        } catch (Exception e) {
            System.out.println("Failed to get class path: " + e.getMessage());
        }
    }