查找运行时Java可用的所有类

查找运行时Java可用的所有类

本文介绍了查找运行时Java可用的所有类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找Java运行时可用的所有类的名称,并且在Guava中通过反射使用了如下代码,从而取得了一些成功:

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

这似乎在某种程度上可以使我获得大约2500个类名,但是并没有找到所有的类名,特别是我确实需要了解的类java.lang,java.util等. /p>

我应该为此使用其他类加载器吗?我也尝试过

Thread.currentThread().getContextClassLoader()

任何指针将不胜感激.谢谢

Ewen

有关解决方案的更多详细信息.

我使用了来自此帖子的更多信息:,以实际实施答案中建议的解决方案.这可以获取所有标准的Java库(大约500 ish),但不能获得我以前在我的方法中使用的库.我可以与以前的产品结合起来,获得所需的一切.

感谢大家的帮助.

解决方案

对于1.5版JVM,您可以使用 http://docs.oracle.com/javase/6/docs/api/java/lang/instrument/Instrumentation.html#getAllLoadedClasses() .

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.

解决方案

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

这篇关于查找运行时Java可用的所有类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:34