List of utility methods to do Class Loader
boolean | addDirectoryToClassLoader(ClassLoader loader, File directory) add Directory To Class Loader try { ClassLoader rootLoader = getRootLoader(loader); if (rootLoader == null) { return false; } else { URL url = directory.toURI().toURL(); Method method = rootLoader.getClass().getMethod("addURL", URL.class); method.invoke(rootLoader, url); ... |
void | addFileToSystemClassLoader(String s) add File To System Class Loader File f = new File(s);
addFileToSystemClassLoader(f);
|
ClassLoader | createClassloader(File libDir, ClassLoader cl) Create a class loader that can load classes from the specified file directory. URLClassLoader urlClassloader = null; if (libDir.exists()) { if (libDir.isDirectory()) { String[] jars = libDir.list(new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".jar") || name.endsWith(".zip"); }); ... |
URLClassLoader | createClassLoader(File root) create Class Loader return new URLClassLoader(new URL[] { root.toURI().toURL() }, Thread.currentThread().getContextClassLoader()); |
ClassLoader | createClassLoader(final String[] cpEntries) Builds a ClassLoader for a given classpath. final URL[] cpUrls = new URL[cpEntries.length]; for (int i = 0; i < cpEntries.length; i++) { String cpEntry = cpEntries[i]; File entry = new File(cpEntry); try { URL url = entry.toURI().toURL(); cpUrls[i] = url; } catch (MalformedURLException ex) { ... |
URLClassLoader | createClassLoader(String fileName) create Class Loader String urlPath = makeUrlPath(fileName); URL url = null; try { url = new URL(urlPath); } catch (MalformedURLException e) { throw new RuntimeException(e); URLClassLoader loader = URLClassLoader.newInstance(new URL[] { url }); ... |
URLClassLoader | createFallbackClassLoader(Collection create Fallback Class Loader List<URL> urls = new ArrayList<URL>(files.size()); for (File file : files) { urls.add(file.toURI().toURL()); return new URLClassLoader(urls.toArray(new URL[urls.size()]), null); |
void | dumpClassLoader(ClassLoader cl, PrintWriter out) dump Class Loader int level = 0; while (cl != null) { String msg = "Servlet ClassLoader[" + level + "]: " + cl.getClass().getName() + ':' + cl.hashCode(); out.println(msg); URL[] urls = getClassLoaderURLs(cl); msg = " URLs:"; out.println(msg); for (int u = 0; u < urls.length; u++) { ... |
Class> | getClass(Object classLoaderMakedObject, File file, String targetClass) get Class URL[] urls = { file.toURI().toURL() };
URLClassLoader classLoader = (URLClassLoader) classLoaderMakedObject.getClass().getClassLoader();
ClassLoader loader = URLClassLoader.newInstance(urls, classLoader);
Class<?> clazz = loader.loadClass(targetClass);
return clazz;
|
InputStream | getClassInputStream(String className, ClassLoader cl) get Class Input Stream return cl.getResourceAsStream(classToResource(className));
|