Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find which jars and in what order are loaded by a classloader?

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.

like image 417
Marina Avatar asked Feb 01 '10 20:02

Marina


People also ask

How can you tell which class a jar is loaded?

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.

Which ClassLoader loads JAR files from JDK?

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.

How do I know if a jar is loaded?

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.

What is the arrangement of ClassLoader in Java environment?

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.


2 Answers

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] 
like image 57
Steve Avatar answered Oct 02 '22 21:10

Steve


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.

like image 25
kdgregory Avatar answered Oct 02 '22 20:10

kdgregory