I could not find a clear answer to this question elsewhere, so I'll try here:
Is there some way (programmatic or other) to get a list of JARs/classes loaded by an Application Classloader in the precise order they were loaded? By Application Classloader I mean the classloader that loads an EAR application in an applications server (WLS, WAS, JBoss...), but obviously, it applies to any classloader.
So, to generalize, what I would like to find out is the list and order of JARs loaded by a specified classloader. Not individual classes, that is easy enough to find out by calling the classloader.getPackages(), but a list of JAR files that were loaded by this classloader.
The, the idea is to use find on the root of your classpath to locate all jars, then runs findclass.sh on all found jars to look for a match. It doesn't handle multi-directories, but if you carefully choose the root you can get it to work.
The Bootstrap class loader loads the basic runtime classes provided by the JVM, plus any classes from JAR files present in the system extensions directory. It is parent to the System class loader. To add JAR files to the system extensions, directory, see Using the Java Optional Package Mechanism.
There are few ways to do this. Use –verbose:class flag with your java command line. This option enables loading and unloading of classes. It shows the jar file from which the class is loaded.
In Java, every ClassLoader has a predefined location from where they load class files. There are following types of ClassLoader in Java: Bootstrap Class Loader: It loads standard JDK class files from rt. jar and other core classes.
Have you tried to use the JVM option -verbose:class
. It displays all loaded JAR files and classes.
Example:
[Opened C:\Program Files\JDK160~1\jre\lib\rt.jar] [Loaded java.lang.Object from C:\Program Files\JDK160~1\jre\lib\rt.jar]
The short answer is no. Classloaders are not required to expose their search logic.
However, if your classloader instance happens to be URLClassLoader or a subclass, then you do have access to the list of jars/directories, via the getURLs()
method. Per the doc for this class, those URLs will be searched in order.
In practice, if you're trying to find out where a class is being loaded from, Steve's answer is probably more useful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With