2

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.

yonran
  • 18,156
  • 8
  • 72
  • 97
  • This is how it works. The bootstrap is native code and does not exist as a `ClassLoader` instance. See http://www.onjava.com/pub/a/onjava/2005/01/26/classloading.html – Jim Garrison Aug 12 '14 at 18:56
  • All bootstrap-resources are loaded from the class-path which is set in system property "sun.boot.class.path" (see also [sun.misc.Launcher](http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/9b8c96f96a0f/src/share/classes/sun/misc/Launcher.java)). But I could only find a "load resource from classpath" implementation [here](http://www.java2s.com/Code/Java/Reflection/ResponsibleforloadingclassfilesfromtheCLASSPATHInspiredbysuntoolsClassPath.htm) which is not even close to an equivalent of `ClassLoader.getResource(String)`. – vanOekel Aug 12 '14 at 19:47

0 Answers0