Dynamic Load By Net

/**
*    
*/
package dynamicLoader;

import java.io.File;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;


/**
* @author daniel
*
*/
public class LoadByUrl {

   /**
    * @param args
    * @throws MalformedURLException    
    */
   public static void main(String[] args) throws MalformedURLException {
    
     new LoadByUrl();
  }

  LoadByUrl(){
    URLClassLoader urlLoader;
    List<URL> urls = new ArrayList<URL>();
    URL[] u;
    
    File file= new File("F:\\Version\\bin\\dynamicLoader\\DemoClass\\");
     try {
      urls.add(file.toURL());
    } catch (MalformedURLException e) {
       // TODO Auto-generated catch block
      e.printStackTrace();
    }
    u = (URL[]) urls.toArray( new URL[urls.size()]);
    
    urlLoader =    URLClassLoader.newInstance(u, LoadByUrl. class.getClassLoader());
    
     try {
      Class cls = urlLoader.loadClass( "DemoClass");
      Object obj = cls.newInstance();
      Method mth= cls.getMethod( "run");
        
    } catch (ClassNotFoundException e) {
       // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (InstantiationException e) {
       // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalAccessException e) {
       // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SecurityException e) {
       // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
       // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

你可能感兴趣的:(职场,休闲,URLClassloader)