You'll probably ask, why would I want to do that - it's because I'm using a class (from an external library) which does stuff in its static initializer and I need to know whether it's been done or not.
I looked at ClassLoader
, but didn't find anything that looked useful. Any ideas?
You can use the
ClassLoader.findLoadedClass()
method. If it returns null, then the class isn't loaded. This way you don't load the class if it wasn't already loaded.WARNING : This code doesn't really work here, in the system ClassLoader,
findLoadedClass()
is protected, you need to override it with your own ClassLoader.Check the link below On the same topic to check if a class is loaded with the system ClassLoader
Very good point from @irreputable :
And I quote :
Resources :
ClassLoader.findLoadedClass()
On the same topic :