class 类与jar 包的动态加载

前段时间要使用class 类与jar 包的动态加载,在网络上找了很多资料,都不完整,现在我把简单整理与修改后的代码贴出来供大家参考, 如果有错误,请大家指出,:)

jar 包加载

package com.sms.util;

import java.net.URLClassLoader;
import java.net.URL;
import java.io.File;
import java.io.IOException;

 

public class NewClassLoader
    extends URLClassLoader {

  public NewClassLoader() {
    super(new URL[0], ClassLoader.getSystemClassLoader());
  }

  public void addPath(String paths) {
    if (paths == null || paths.length() <= 0) {
      return;
    }
    String separator = System.getProperty("path.separator");
    String[] pathToAdds = paths.split(separator);
    for (int i = 0; i < pathToAdds.length; i++) {
      if (pathToAdds[i] != null && pathToAdds[i].length() > 0) {
        try {
          File pathToAdd = new File(pathToAdds[i]).getCanonicalFile();
          System.out.println(pathToAdd.toURL());
          addURL(pathToAdd.toURL());
        }
        catch (IOException e) {
          Constants.exceptionHandling(e);
        }
      }
    }
  }

}

 

类加载

import java.net.URLClassLoader;
import java.net.URL;
import java.io.File;
import java.io.IOException;

 

public class NewClassLoader
    extends URLClassLoader {

  public NewClassLoader() {
    super(new URL[0], ClassLoader.getSystemClassLoader());
  }

  public void addPath(String paths) {
    if (paths == null || paths.length() <= 0) {
      return;
    }
    String separator = System.getProperty("path.separator");
    String[] pathToAdds = paths.split(separator);
    for (int i = 0; i < pathToAdds.length; i++) {
      if (pathToAdds[i] != null && pathToAdds[i].length() > 0) {
        try {
          File pathToAdd = new File(pathToAdds[i]).getCanonicalFile();
          System.out.println(pathToAdd.toURL());
          addURL(pathToAdd.toURL());
        }
        catch (IOException e) {
          Constants.exceptionHandling(e);
        }
      }
    }
  }

}

你可能感兴趣的:(java,.net)