Chinaunix首页 | 论坛 | 博客
  • 博客访问: 861606
  • 博文数量: 182
  • 博客积分: 1992
  • 博客等级: 上尉
  • 技术积分: 1766
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-18 11:49
文章分类

全部博文(182)

文章存档

2019年(1)

2016年(5)

2015年(29)

2014年(38)

2013年(21)

2012年(36)

2011年(52)

我的朋友

分类: Java

2012-03-30 16:18:17

1.参考老外的

点击(此处)折叠或打开

  1. public class ClassPathUpdater {
  2.     /** Used to find the method signature. */
  3.     private static final Class[] PARAMETERS = new Class[] { URL.class };

  4.     /** Class containing the private addURL method. */
  5.     private static final Class CLASS_LOADER = URLClassLoader.class;

  6.     /**
  7.      * Adds a new path to the classloader. If the given string points to a file,
  8.      * then that file's parent file (i.e., directory) is used as the directory
  9.      * to add to the classpath. If the given string represents a directory, then
  10.      * the directory is directly added to the classpath.
  11.      *
  12.      * @param s
  13.      * The directory to add to the classpath (or a file, which will
  14.      * relegate to its directory).
  15.      */
  16.     public static void add(String s) throws IOException, NoSuchMethodException,
  17.             IllegalAcces***ception, InvocationTargetException {
  18.         add(new File(s));
  19.     }

  20.     /**
  21.      * Adds a new path to the classloader. If the given file object is a file,
  22.      * then its parent file (i.e., directory) is used as the directory to add to
  23.      * the classpath. If the given string represents a directory, then the
  24.      * directory it represents is added.
  25.      *
  26.      * @param f
  27.      * The directory (or enclosing directory if a file) to add to the
  28.      * classpath.
  29.      */
  30.     public static void add(File f) throws IOException, NoSuchMethodException,
  31.             IllegalAcces***ception, InvocationTargetException {
  32.         f = f.isDirectory() ? f : f.getParentFile();
  33.         add(f.toURI().toURL());
  34.     }

  35.     /**
  36.      * Adds a new path to the classloader. The class must point to a directory,
  37.      * not a file.
  38.      *
  39.      * @param url
  40.      * The path to include when searching the classpath.
  41.      */
  42.     public static void add(URL url) throws IOException, NoSuchMethodException,
  43.             IllegalAcces***ception, InvocationTargetException {
  44.         Method method = CLASS_LOADER.getDeclaredMethod("addURL", PARAMETERS);
  45.         method.setAccessible(true);
  46.         method.invoke(getClassLoader(), new Object[] { url });
  47.     }

  48.     private static URLClassLoader getClassLoader() {
  49.         return (URLClassLoader) ClassLoader.getSystemClassLoader();
  50.     }
  51. }
ClassPathUpdater.add("E:\\workspaces\\MyDemos\\MyClass.class");
        String path = "E:\\workspaces\\MyDemos\\";
        URL url = new URL("file", null, path);
        URLClassLoader loader = new URLClassLoader(new URL[] {url});
        Class clazz = loader.loadClass("MyClass");

2、rewrite ClassLoader

点击(此处)折叠或打开

  1. static class MyClassLoader extends ClassLoader {
  2.         @Override
  3.         public Class<?> findClass(String name) {
  4.             try {
  5.                 FileInputStream in = new FileInputStream(name);
  6.                 ArrayList<Byte> l = new ArrayList<Byte>();
  7.                 int c;
  8.                 while ((c = in.read()) != -1) {
  9.                     l.add(new Byte((byte)c));
  10.                 }
  11.                 byte[] b = new byte[l.size()];
  12.                 for (int i = 0; i < l.size(); i++) {
  13.                     b[i] = l.get(i).byteValue();
  14.                 }
  15.                 
  16.                 return defineClass(b, 0, b.length);
  17.             } catch (Exception e) {
  18.                 // TODO Auto-generated catch block
  19.                 e.printStackTrace();
  20.             }
  21.             return null;
  22.         }
  23.     }

MyClassLoader mcl = new MyClassLoader();
        clazz = mcl.loadClass("E:\\workspaces\\MyDemos\\MyClass.class");
阅读(2862) | 评论(0) | 转发(0) |
0

上一篇:Java jni笔记

下一篇:restart task主要过程

给主人留下些什么吧!~~