日期:2014-05-20 浏览次数:20799 次
import java.net.URL; import java.net.URLClassLoader; public class ClassLoaderTest { public static void main(String[] args) throws Exception { URL[] url = { new URL("file:D:/ant/gao-util/dest/gao-jpa.jar") }; URLClassLoader classloader = new URLClassLoader(url); Class<?> clazz = classloader.loadClass("net.blogjava.frankiegao123.jpa.query.DateQuery"); Object dateQuery = clazz.newInstance(); System.out.println(dateQuery); } }
------解决方案--------------------
public static void main(String[] args) { try { URL url = new URL("file:///D:/workspace/test/bin/test.jar"); URLClassLoader urlCL = new URLClassLoader(new URL[] { url }); Class c = urlCL.loadClass("TestClassA"); Method m = c.getMethod("method"); m.invoke(c.newInstance(), null); } catch (Exception e) { e.printStackTrace(); } }