In a Java application, the Bootstrap Class Loader (which is used to load classes from JRE such as rt.jar) is null. That is, java.util.ArrayList.class.getClassLoader() == null
.
This is annoying because when writing a library that uses reflection, one must use the appropriate class loader, which sometimes is null. So in case the ClassLoader is null, a library should call Class.forName(String, false, classLoader)
instead of classLoader.loadClass(String)
. By the way, the static method Proxy.newProxyInstance(classLoader, interfaces, invocationHandler)
works fine when the classLoader is null.
But is there any equivalent of ClassLoader.getResource(String)
or ClassLoader.getResources(String)
that works even when the classLoader is null? The closest I can think of is new ClassLoader(classLoader).getResource(String)
, but that uses extra SecurityManager permissions than necessary.