6

There is a method called findBootstrapClass for a ClassLoader that returns a Class if it is bootstrapped. Is there a way to find classes has been loaded?

prmottajr
  • 1,816
  • 1
  • 13
  • 22
zcaudate
  • 13,998
  • 7
  • 64
  • 124

1 Answers1

6

You could try to first get the bootstrap class loader by e.g. calling

ClassLoader bootstrapLoader = ClassLoader.getSystemClassLoader().getParent();

and then get the classes of this class loader as explained here: How can I list all classes loaded in a specific class loader.

But note, that getting the bootstrap class loader is not reliable, because it may not explicitly exist. So ClassLoader.getSystemClassLoader().getParent() may return null, as explained in the Javadoc of ClassLoader#getParent():

Returns the parent class loader for delegation. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class loader's parent is the bootstrap class loader.

Community
  • 1
  • 1
Balder
  • 8,623
  • 4
  • 39
  • 61
  • 4
    On most implementations between Java 2 and Java 8, inclusive, the expression `ClassLoader.getSystemClassLoader().getParent()` will give you the *extension class loader*, so you would have to use `ClassLoader.getSystemClassLoader().getParent().getParent()` to get the bootstrap loader, though, it would be much simpler to use `Object.class.getClassLoader()`, as if not `java.lang.Object` has been loaded through the bootstrap loader, what else should have been? But, well, I don’t know any environment where the bootstrap loader is not represented by `null`… – Holger Feb 08 '18 at 17:41