我想象中这个应该自己写类加载器来实现,这种思路是否可以解决这个问题?有做过这方面需求的大侠来肯定一下吗?

解决方案 »

  1.   

    完全是可以的,以下是在common.FileSystemClassLoader实现的主方法:
    请注意String pathName = currentRoot + File.separatorChar+ className.replace('.', File.separatorChar) + ".class";这句话.public byte[] findClassBytes(String className) {
    try {
    String pathName = currentRoot + File.separatorChar
    + className.replace('.', File.separatorChar) 
                                         + ".class";
    FileInputStream inFile = new FileInputStream(pathName);
    byte[] classBytes = new byte[inFile.available()];
    inFile.read(classBytes);
    return classBytes;
    } catch (java.io.IOException ioEx) {
    return null;
    }
    }public Class findClass(String name) throws ClassNotFoundException {
    byte[] classBytes = findClassBytes(name);
    if (classBytes == null) {
    throw new ClassNotFoundException();
    } else {
    return defineClass(name, classBytes, 0, classBytes.length);
    }
    }public Class findClass(String name, byte[] classBytes)
    throws ClassNotFoundException {
    if (classBytes == null) {
    throw new ClassNotFoundException("(classBytes==null)");
    } else {
    return defineClass(name, classBytes, 0, classBytes.length);
    }
    }public void execute(String codeName, byte[] code) {
    Class klass = null;
    try {
    klass = findClass(codeName, code);
    TaskIntf task = (TaskIntf) klass.newInstance();
    task.execute();
    } catch (Exception exception) {
    exception.printStackTrace();
    }
    }