使用URLClassLoader加载.class文件

时间:2015-09-24 21:18:38

标签: java urlclassloader dynamic-class-loaders

我之前已经问过这个问题:

How to use URLClassLoader to load a *.class file?

但由于缺乏榜样,我不太懂。我目前正在处理一个项目并试图加载用户给定的.class对象,这些对象可以位于机器上的任何目录中。

//Create URL to hash function class file
URL url_hashFunctionPath = new URL("file:///" + _sHashFunctionFilePath);

//Packet URL to a URL array to be used by URLClassLoader
URL[] urlA_hashFunctionPath = {url_hashFunctionPath};

//Load URL for hash function via a URL class loader
URLClassLoader urlCl_hashFunctionClassLoader = new URLClassLoader(urlA_hashFunctionPath);

//Load user hash function into class to initialize later (TEST: HARD CODE FOR NOW)
m_classUserHashClass = urlCl_hashFunctionClassLoader.loadClass(_sUserHashClassName);

最后一行给了我一个ClassNotFoundException,来自我的实验&理解用户给定的类函数必须在类路径中?

PS:第一次发帖问题随时可以纠正我没有遵循适当方式的地方。

//解

我在[WillShackleford] [1]的慷慨帮助下得到的解决方案,此解决方案可以在给定的文件路径中加载.class文件。有关更多信息,请参阅代码及其给出的注释。

//The absolute file path to the class that is to be loaded (_sHashFunctionFilePath = absolute file path)
String pathToClassFile = _sHashFunctionFilePath;
System.out.println("File to class: " + _sHashFunctionFilePath);

//Declare the process builder to execute class file at run time (Provided filepath to class)
ProcessBuilder pb = new ProcessBuilder("javap", pathToClassFile);
try
{
    //Start the process builder
    Process p = pb.start();

    //Declare string to hold class name
    String classname = null;
    //Declare buffer reader to read the class file & get class name
    try(BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())))
    {
        String line;
        while(null != (line = br.readLine()))
        {
            if(line.startsWith("public class"))
            {
                classname = line.split(" ")[2];
                break;
            }
        }
        System.out.println("classname = " + classname);
    }
    catch(IOException _error)
    {

    }

    //Declare file path to directory containing class to be loaded
    String pathToPackageBase = pathToClassFile.substring(0, pathToClassFile.length() - (classname + ".class").length());
    System.out.println("pathToPackageBase = " + pathToPackageBase);

    try
    {
        //Create class to hold the class to be loaded via a URL class loader
        Class clss = new URLClassLoader(
                new URL[]{new File(pathToPackageBase).toURI().toURL()}
        ).loadClass(classname);

        //Create ab object/instance of said class
        Object test = clss.newInstance();

        //Declare & create requested method from user hash function class (Method is work & takes no arguments)
        Method method = clss.getMethod("work", null);
        method.invoke(test, null);
    }

2 个答案:

答案 0 :(得分:3)

在目录/home/shackle/somedir/classes/pkg中,我有一个文件Test.class,它是从带有package pkg;的java文件创建的,例如:

package pkg;

public class Test {

    public String toString() {
        return "secret_string";
    }
}

然后我加载它:

System.out.println(new URLClassLoader(
        new URL[]{new File("/home/shackle/somedir/classes").toURI().toURL()}
).loadClass("pkg.Test").newInstance().toString());

请注意,我没有将pkg / Test放在URL字符串中,但是load class参数具有pkg。前缀。

您可以直接从文件中获取类名:

Class clsReaderClss = ClassLoader.getSystemClassLoader().loadClass("jdk.internal.org.objectweb.asm.ClassReader");
System.out.println("clsReaderClss = " + clsReaderClss);
Constructor con = clsReaderClss.getConstructor(InputStream.class);
Object reader = con.newInstance(new FileInputStream(directFile));
Method m = clsReaderClss.getMethod("getClassName");
String name = m.invoke(reader).toString().replace('/', '.');
System.out.println("name = " + name);

不需要访问内部类的替代方案。

String pathToClassFile = "/home/shackle/somedir/classes/pkg/Test.class";
ProcessBuilder pb = new ProcessBuilder("javap",pathToClassFile);
Process p = pb.start();
String classname = null;
try(BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
   String line;
   while(null != (line = br.readLine())) {
       if(line.startsWith("public class")) {
           classname = line.split(" ")[2];
           break;
       }
   }
}
System.out.println("classname = " + classname);

然后可以加载类:

String pathToPackageBase = pathToClassFile.substring(0, pathToClassFile.length() - (classname + ".class").length());
System.out.println("pathToPackagBase = " + pathToPackageBase);
Class clss = new URLClassLoader(
        new URL[]{new File(pathToPackageBase).toURI().toURL()}
).loadClass(classname);
System.out.println(clss.newInstance().toString());

答案 1 :(得分:0)

您的_sHashFunctionFilePath需要从中删除目标类的包名称,因此ClassLoader会在_sHashFunctionFilePath + package.name + HashFunction.class中查找文件。如果不这样做,ClassLoader将无法找到该文件。

因此,如果目标类在my.great.HashFunction中为HashFunction.class,则如果要使用URLClassLoader,则需要位于名为my/great/的目录中。然后,如果在/path/to中实际找到.class文件,则使用file:///作为URLClassLoader的/path/to/my/great/HashFunction.class网址。