public static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
}
catch (Throwable ex) {
// Cannot access thread context ClassLoader - falling back...
}
if (cl == null) {
// No thread context class loader -> use class loader of this class.
cl = ClassUtils.class.getClassLoader();
if (cl == null) {
// getClassLoader() returning null indicates the bootstrap ClassLoader
try {
cl = ClassLoader.getSystemClassLoader();
}
catch (Throwable ex) {
// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
}
}
}
return cl;
}获取默认ClassLoader方法
最新推荐文章于 2023-01-26 16:52:32 发布
本文介绍了一种在Java中获取默认类加载器的方法。通过尝试获取当前线程上下文类加载器,如果不可用,则使用当前类的类加载器,并最终回退到系统类加载器。这种方法适用于解决类加载路径问题。
33万+

被折叠的 条评论
为什么被折叠?



