读书人

动态加载种

发布时间: 2013-02-24 17:58:56 作者: rapoo

动态加载类
package com.joshua.test; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.security.AccessController; import java.util.LinkedList; import java.util.List; import java.util.StringTokenizer; public class ClassLoaderTest { private static final String CLASSPATH_SEPARATOR = System.getProperty("path.separator"); private static ClassLoader classLoader; static { String classPath = System.getProperty("java.class.path"); String homePath = getHomePath(classPath); File[] jars = new File(homePath + "/lib").listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return dir.canRead() & name.endsWith(".jar"); } }); // Add jars in lib dir into classpath. final List<URL> jarPaths = new LinkedList<URL>(); StringBuilder classPathSB = new StringBuilder(classPath); for (File jar : jars) { try { jarPaths.add(jar.toURI().toURL()); classPathSB.append(CLASSPATH_SEPARATOR); classPathSB.append(jar.getPath()); } catch (MalformedURLException e) { e.printStackTrace(); } } // Modify the classpath System.setProperty("java.class.path", classPathSB.toString()); classLoader = AccessController.doPrivileged( new java.security.PrivilegedAction<URLClassLoader>() { @Override public URLClassLoader run() { return new URLClassLoader(jarPaths.toArray(new URL[jarPaths.size()])); } } ); Class clazz = null; try { clazz = classLoader.loadClass("$全类名"); } catch (ClassNotFoundException e) { e.printStackTrace(); } if (clazz == null) System.exit(-1); Object loadedInstance = null; try { loadedInstance = clazz.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (loadedInstance == null) System.exit(-1); } public static String getHomePath(String classPath) { StringTokenizer st = new StringTokenizer(classPath, File.pathSeparator); String binPath = null; if (st.countTokens() > 0) { binPath = st.nextToken(); } try { return new File(binPath).getCanonicalFile().getParent(); } catch (IOException e) { e.printStackTrace(); return ""; } } }

?

?

读书人网 >编程

热点推荐