美文网首页
java动态加载

java动态加载

作者: jacksu在简书 | 来源:发表于2016-05-25 08:25 被阅读144次
    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);  
    

    相关文章

      网友评论

          本文标题:java动态加载

          本文链接:https://www.haomeiwen.com/subject/hxlzrttx.html