How to print current ClassLoader with or without Spring
Now we explain how to print a class loader associated to current thread. We can use Spring, and with one row of code, we solve the problem! We use the ClassLoaderUtils class.
For example:
System.out.println(
org.springframework.util.ClassLoaderUtils
.showClassLoaderHierarchy(
Thread.currentThread().getContextClassLoader()));
If we don’t want to have any library dependency (only java >=1.4.2 code):
ClassLoader cl = Thread.currentThread().getContextClassLoader();
int indent = 1;
while (cl != null) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < indent; i++) {
buf.append("t");
}
buf.append("[").append(cl).append("] hashCode=")
.append(cl.hashCode()).append("n");
cl = cl.getParent();
indent++;
System.out.println(buf.toString());
}
