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

I am trying to implement some unit testing for an old framework. I am attempting to mock out the database layer. Unfortunately our framework is a bit old and not quite using best practices so there is no clear separation of concerns. I am bit worried that trying to mock out the database layer might make the JVM load a huge number of classes that won't even be used.

I don't really understand class loaders that well so this might not be a problem. Is there a way to take a peak at all the classes a particular ClassLoader has loaded to prove what is going on under the hood?


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

You can create your own Classloader and use that to load during the unit test. Have your own custom Classloader print out what it's doing.

Or if you just want to know which classes are loaded, do:

java -verbose:class

I am not sure. But there is one way I see it could be done. It maybe overrly ridiculous though. You can try aspects and put a pointcut for loadclass. Also maybe the jvm argument -verbose maybe helpful.

As an alternative way, for a particular Class-loader as you mentioned, you can use this code snippet. Just change value of obj variable if you want.

Object obj = this;
ClassLoader classLoader = obj.getClass().getClassLoader();
File file = new File("classloderClasses.txt");
if (file.exists()) {
if (classLoader != null) {
    try {
        Class clClass = classLoader.getClass();
        while (clClass != ClassLoader.class) {
            clClass = clClass.getSuperclass();
        java.lang.reflect.Field classesField = clClass.getDeclaredField("classes");
        Vector classes = (Vector) classesField.get(classLoader);
        FileOutputStream fos = new FileOutputStream("classloderClasses.txt", true);
        fos.write(("******************** " + classLoader.toString() + " ******************** " + "\n").getBytes());
    } catch (Exception exception) {
        // TODO

You use the -Xlog option to configure or enable logging with the Java Virtual Machine (JVM) unified logging framework. The advantage is that you can write results to text file


-Xlog[:[what][:[output][:[decorators][:output-options [,...]]]]]

In Unified Logging syntax, -verbose:class equals -Xlog:class+load=info

For example

java -Xlog:class+load=info:classloaded.txt

Ocarle doc

Need Your Help

Dynamically add script tag with src that may include document.write

javascript src document.write

I want to dynamically include a script tag in a webpage however I have no control of it's src so src="source.js" may look like this.

Are there some disadvantages in using "a lot" of LINQ to Objects statements?

c# .net performance linq

I come from Java programming and switching to C# programming I discovered the extreme powerful of LINQ.