How do I get a list of all package names loaded by the JVM

I need to get names of all java packages loaded by the JVM. This is to display a package browser like the ones found in IDEs. I can get the package list of the current classloader and its ancestors by accessing protected "packages" field of the ClassLoader class. But i'm unable to get the packages loaded by other webapps as they have their own class loaders. I'm testing this on Weblogic server


The expected behavior of the Weblogic security model is that you would not have access to the other web applications' class loaders. This is not really something that you will be able to get around - see this article for more information.


You need to walk the tree of classloaders up by using getParent() , find all classes which extend ClassLoader, find all current instances (the debug API should help here). But that probably won't work for Web Servers because of the security policy (web apps are not allowed to peek at each other).

For Tomcat, there is an option to log all classes as they are loaded. This slows down a server pretty much but it might be an option on the development server.

That said, I'm pretty curious why you would need that. The most simple solution would be to list all JAR files, which your app brings along, with jar tvf and strip the last part of the path (the class file).


The only way I can think of doing this would be to modify each webapp so that you can send each a request for their loaded class information. You could then create a new webapp that combines the responses from the existing webapps for display.

If you don't need this information in some nice UI then the Sun JVM has number of -XX vm options that will show you what's going on with regards to class loading.

http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp

I'm not that familiar with JRockit but I'd be surprised if it didn't have similar options.

链接地址: http://www.djcxy.com/p/58266.html

上一篇: 无法创建cutom类加载器

下一篇: 如何获取JVM加载的所有软件包名称的列表