views:

49

answers:

2

hi all

how can i get list of all available classes in classpath at runtime?!

as i know in eclipse IDE by pressing crtl+shift+T it is possible to do such thing

is there any method in java to get it done?

thanks in advance

+1  A: 

You can get all classpath roots by passing an empty String into ClassLoader#getResources().

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

You can construct a File based on URL as follows:

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

You can use File#listFiles() to get a list of all files in the given directory:

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

You can use the standard java.io.File methods to check if it's a directory and/or to grab the filename.

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

Depending on the sole functional requirement, I guess that Google Reflections is much more exactly what you want.

BalusC