在飞行类装载罐子 [英] On the fly class loading with jars

查看:109
本文介绍了在飞行类装载罐子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用以下方法扩展类的ClassLoader

I've got a ClassLoader extending class with following method

@Override
public Class<?> findClass(String className) throws ClassNotFoundException {
    try {
        /**
        * Get a bytecode from file
        */

        byte b[] = fetchClassFromFS(pathtobin + File.separator
            + className.replaceAll("\\.", escapeSeparator(File.separator)) + ".class");
        return defineClass(className, b, 0, b.length);
    } catch (FileNotFoundException ex) {
        return super.findClass(className);
    } catch (IOException ex) {
        return super.findClass(className);
    }
}

你可以看到使用的defineClass()方法它的父级 - ClassLoader。问题是,当我正在尝试执行一个类'(我接受我的ClassLoader扩展 - 让它成为ru.xmppTesting.test.Disco)方法getMethods(),同时获取此类的实例我得到以下

That as u can see uses defineClass() method from its parent - ClassLoader. The issue is when i'm trying to execute a class' (i recieve with my ClassLoader extension - let it be ru.xmppTesting.test.Disco) method getMethods() while getting an instance of this class i get the following

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/Header
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
    at java.lang.Class.privateGetPublicMethods(Unknown Source)
    at java.lang.Class.getMethods(Unknown Source)
    at DOTGraphCreator.createGraphFromClasses(DOTGraphCreator.java:85)
    at DOTGraphCreator.generateDotGraphFile(DOTGraphCreator.java:56)
    at DOTGraphCreator.main(DOTGraphCreator.java:46)
Caused by: java.lang.ClassNotFoundException: org.apache.http.Header
    at java.lang.ClassLoader.findClass(Unknown Source)
    at SourceClassLoader.findClass(SourceClassLoader.java:27)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 7 more

据我所知,这是因为无法找到类org.apache.http.Header。因为它不是。

As far as i can see that is because class org.apache.http.Header could not be found as defined. Because it is not.

所以这里有一个问题:

我该如何定义和链接这个标题?类(以及我的.jar库中的许多其他类)以及ru.xmppTesting.test.Disco的定义以及其他类似的动态定义它们?

how can and must i define and link this Header class (and lots of others from my .jar libs) along with definition of ru.xmppTesting.test.Disco and others similar to have them defined on the fly?

推荐答案

如果您要从已加载dinamic的类中导入 org.apache.http.Header ,则需要在类路径中访问它。

If your are importing org.apache.http.Header from your dinamic loaded class, you need it to be accesible at your classpath.

如果您不想在类路径中加载所有可能需要的jar,您可以尝试使用我发现的hack 这里

If you don't want to load all the potentially needed jars on your classpath, you could try with a hack i have found here:

import java.lang.reflect.*;
import java.io.*;
import java.net.*;

public class ClassPathHacker {

private static final Class[] parameters = new Class[]{URL.class};

public static void addFile(String s) throws IOException {
    File f = new File(s);
    addFile(f);
}//end method

public static void addFile(File f) throws IOException {
    addURL(f.toURL());
}//end method


public static void addURL(URL u) throws IOException {

    URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
    Class sysclass = URLClassLoader.class;

    try {
        Method method = sysclass.getDeclaredMethod("addURL",parameters);
        method.setAccessible(true);
        method.invoke(sysloader,new Object[]{ u });
    } catch (Throwable t) {
        t.printStackTrace();
        throw new IOException("Error, could not add URL to system classloader");
    }//end try catch

}//end method

}//end class

但是,我必须说,它无法移植到某些JVM(并不总是SystemClassLoader是URLClassLoader的子类)...

But, I must say, it could not be portable to some JVMs (not always the SystemClassLoader is a subclass of URLClassLoader)...

* 编辑:* 事实上,正如您已经用自己的类加载器替换了类加载器,也许您有一些麻烦......

* * In fact, as you have replaced the classloader with your own, perhaps you have some troubles...

这篇关于在飞行类装载罐子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆