Java tutorial
//package com.java2s; //License from project: Apache License import java.lang.instrument.Instrumentation; import java.util.Map; import java.util.WeakHashMap; public class Main { /** * * @param name * the fully qualified classname * @return */ static Map<String, ClassLoader> classLoaderCache = new WeakHashMap<String, ClassLoader>(); private static Instrumentation instrumentation; static synchronized public ClassLoader getCachedClassLoader(String name) { if (classLoaderCache.get(name) != null) return classLoaderCache.get(name); ClassLoader cl = getClassLoaderByName(name); classLoaderCache.put(name, cl); return cl; } /** * Use instrumentation to get the class loader for a given class. * * @param name * the fully qualified classname * @return the classloader */ static public ClassLoader getClassLoaderByName(String name) { Class<?>[] allLoadedClasses = instrumentation.getAllLoadedClasses(); ClassLoader cl = null; for (Class<?> c : allLoadedClasses) { if (c.getName().equals(name)) { cl = c.getClassLoader(); break; } } return cl; } }