Find all classes availible at runtime java

2019-02-20 02:25发布

问题:

I am attempting to find the names of all classes available at runtime in Java and have had some success using reflection in Guava with code such as:

ClassPath classPath = ClassPath.from(ClassLoader.getSystemClassLoader());
contextClasses = classPath.getAllClasses();

This appears to work to some extent getting me about 2500 class names, however it does not find all of them, in particular the java.lang, java.util, etc. classes which i really do need to know about.

Is there a different class loader I should be using for this? I have also tried

Thread.currentThread().getContextClassLoader()

Any pointers would be much appreciated. Thanks

Ewen

Further detail about solution.

I used further information from this post: How can I list all classes loaded in a specific class loader to actually implement the solution suggested in the answer. This seams to get all of the standard java libraries (some 500 ish) but not the ones I go in my method previously. I could combine with the previous to get everything I need.

Thanks to everyone for their help.

回答1:

For a post 1.5 JVM, you can use http://docs.oracle.com/javase/6/docs/api/java/lang/instrument/Instrumentation.html#getAllLoadedClasses().



标签: java jvm guava