How to List All Classes Loaded in a Specific Class Loader

How can I list all classes loaded in a specific class loader

Instrumentation.getInitiatedClasses(ClassLoader) may do what you want.

According to the docs:

Returns an array of all classes for which loader is an initiating loader.

I'm not sure what "initiating loader" means though. If that does not give the right result try using the getAllLoadedClasses() method and manually filtering by ClassLoader.


How to get an instance of Instrumentation

Only the agent JAR (which is separate from the application JAR) can get an instance of the Instrumentation interface. A simple way to make it available to the application is to create an agent JAR containing one class with a premain method that does nothing but save a reference to the Instrumentation instance in the system properties.

Example agent class:

public class InstrumentHook {

public static void premain(String agentArgs, Instrumentation inst) {
if (agentArgs != null) {
System.getProperties().put(AGENT_ARGS_KEY, agentArgs);
}
System.getProperties().put(INSTRUMENTATION_KEY, inst);
}

public static Instrumentation getInstrumentation() {
return (Instrumentation) System.getProperties().get(INSTRUMENTATION_KEY);
}

// Needn't be a UUID - can be a String or any other object that
// implements equals().
private static final Object AGENT_ARGS_KEY =
UUID.fromString("887b43f3-c742-4b87-978d-70d2db74e40e");

private static final Object INSTRUMENTATION_KEY =
UUID.fromString("214ac54a-60a5-417e-b3b8-772e80a16667");

}

Example manifest:

Manifest-Version: 1.0
Premain-Class: InstrumentHook

The resulting JAR must then be referenced by the application and specified on the command line (with the -javaagent option) when launching the application. It might be loaded twice in different ClassLoaders, but that is not a problem since the system Properties is a per-process singleton.

Example application class

public class Main {
public static void main(String[] args) {
Instrumentation inst = InstrumentHook.getInstrumentation();
for (Class<?> clazz: inst.getAllLoadedClasses()) {
System.err.println(clazz.getName());
}
}
}

How to get a list of all classes that are loaded by the bootstrap class loader?

Watch the method getInitiatedClasses:

http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/Instrumentation.html

Is there a way to get which classes a ClassLoader has loaded?

Be warned that using

java -verbose

Will produce an enormous amount of output. Log the output to a file and then use grep. If you have the 'tee' filter you could try this:

java -verbose | tee classloader.log
grep class classloader.log

How to explore which classes are loaded from which JARs?

Passing the -verbose:class switch to the java command will print each class loaded and where it was loaded from.

Joops is also a nice tool for finding missing classes ahead of time.

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

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.



Related Topics



Leave a reply



Submit