On compile or run, we usually have a lot of class/jar dependencies, and the database driver's jar is just one of them. So, does
Class.forName(X)
have to scan all these .class/.jar file names to locate proper driver class named X? If not, does Class.forName
has any rule/algorithm to find particular class?
It delegates to the ClassLoader
of the calling class. In most cases the answer is yes but there are ClassLoader
implementations with more sophisticated rules e.g. OSGI which uses dynamic modules or JEE Server Application Classloading.
public static Class<?> forName(String className) throws ClassNotFoundException {
Class<?> caller = Reflection.getCallerClass();
return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
}