Search code examples
javaunit-testingjvmclassloader

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?


Solution

  • 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