java动态加载
2016-05-25 本文已影响144人
jacksu在简书
public abstract class AbstractAction {
public abstract String action();
}
public class TestAction extends AbstractAction{
public String action() {
System.out.println("I am working ! ");
return "this ActionTest class";
}
}
String s = "file://D:\Style.jar";***//jar所在的文件的URL*
URL url = new URL(s);
URLClassLoader myClassLoader = new URLClassLoader(new URL[] { url }, Thread.currentThread() .getContextClassLoader());
Class<? extends AbstractAction> myClass = (Class<? extends AbstractAction>) myClassLoader.loadClass("com.java.jarloader.TestAction");
AbstractAction action = (AbstractAction) myClass.newInstance();
String str = action.action();
System.out.println(str);