需要通过引导类加载器加载一些类

发布于 2024-10-20 06:59:21 字数 3716 浏览 6 评论 0原文

我有以下场景:

我有一个 byte[] ,其中包含一个类的 .class 数据(从文件系统加载)

并且我有另一个byte[] 之前已序列化到某个其他流。

首先使用我的自定义类加载器加载 .class 文件的 byte[] ,即:

public class MainSearchClassLoader extends ClassLoader
{

    public MainSearchClassLoader()
    {
        super(MainSearchClassLoader.class.getClassLoader());
    }

    public Class<?> findClass(String name) throws ClassNotFoundException
    {
        try
        {
            byte[] bytecode = FileUtil.readClassByteCode();
            return super.defineClass(ReflectionUtil.getStubBinaryClassName() , bytecode, 0, bytecode.length);
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }
}

然后我尝试使用以下代码反序列化该实例

public static Object getObjectFromBytes(final byte[] bytes)
    {
        Object object = null;
        try
        {
            object = new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject();
        } catch (final Exception ioe)
        {
            ioe.printStackTrace();
        }
        return object;
    }

:接受序列化字节并应该返回实例(使用我的自定义类加载器预加载的类)..我得到以下异常:

11/03/06 14:23:27 oracle.classloader.util.AnnotatedClassNotFoundException: 

      Missing class: mainSearchObjects.dc_index

    Dependent class: java.io.ObjectInputStream
             Loader: jre.bootstrap:1.5.0_06
        Code-Source: unknown
      Configuration: jre bootstrap

This load was initiated at MainSearch.web.MainSearch:0.0.0 using the Class.forName() method.

The missing class is not available from any code-source or loader in the system.
11/03/06 14:23:27   at oracle.classloader.PolicyClassLoader.handleClassNotFound (PolicyClassLoader.java:2068) [/D:/jdevstudio10134/j2ee/home/lib/pcl.jar (from system property java.class.path), by sun.misc.Launcher$AppClassLoader@14916158]
    at oracle.classloader.PolicyClassLoader.internalLoadClass (PolicyClassLoader.java:1679) [/D:/jdevstudio10134/j2ee/home/lib/pcl.jar (from system property java.class.path), by sun.misc.Launcher$AppClassLoader@14916158]
    at oracle.classloader.PolicyClassLoader.loadClass (PolicyClassLoader.java:1635) [/D:/jdevstudio10134/j2ee/home/lib/pcl.jar (from system property java.class.path), by sun.misc.Launcher$AppClassLoader@14916158]
    at oracle.classloader.PolicyClassLoader.loadClass (PolicyClassLoader.java:1620) [/D:/jdevstudio10134/j2ee/home/lib/pcl.jar (from system property java.class.path), by sun.misc.Launcher$AppClassLoader@14916158]
    at java.lang.ClassLoader.loadClassInternal (ClassLoader.java:319) [jre bootstrap, by jre.bootstrap:1.5.0_06]
    at java.lang.Class.forName0 (Native method) [unknown, by unknown]
    at java.lang.Class.forName (Class.java:242) [jre bootstrap, by jre.bootstrap:1.5.0_06]
    at java.io.ObjectInputStream.resolveClass (ObjectInputStream.java:574) [jre bootstrap, by jre.bootstrap:1.5.0_06]
    at java.io.ObjectInputStream.readNonProxyDesc (ObjectInputStream.java:1538) [jre bootstrap, by jre.bootstrap:1.5.0_06]
    at java.io.ObjectInputStream.readClassDesc (ObjectInputStream.java:1460) [jre bootstrap, by jre.bootstrap:1.5.0_06]
    at java.io.ObjectInputStream.readOrdinaryObject (ObjectInputStream.java:1693) [jre bootstrap, by jre.bootstrap:1.5.0_06]
    at java.io.ObjectInputStream.readObject0 (ObjectInputStream.java:1299) [jre bootstrap, by jre.bootstrap:1.5.0_06]
    at java.io.ObjectInputStream.readObject (ObjectInputStream.java:339) [jre bootstrap, by jre.bootstrap:1.5.0_06]
...........

我明白..反序列化代码使用的引导类加载器看不到由其子级之一(我的类加载器)加载的类,我认为这是一种正确的行为,不是吗?

那么,这个问题就没有办法解决了吗??

I've the following scenario:

I've a byte[] that contains the .class data of a class (loaded from the file system)

And I have another byte[] of this some object of this class that was previously Serialized to some other stream.

First do load the byte[] of the .class file with my custom class loader which is:

public class MainSearchClassLoader extends ClassLoader
{

    public MainSearchClassLoader()
    {
        super(MainSearchClassLoader.class.getClassLoader());
    }

    public Class<?> findClass(String name) throws ClassNotFoundException
    {
        try
        {
            byte[] bytecode = FileUtil.readClassByteCode();
            return super.defineClass(ReflectionUtil.getStubBinaryClassName() , bytecode, 0, bytecode.length);
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }
}

Then I am trying to de-serialize this instance using the following code:

public static Object getObjectFromBytes(final byte[] bytes)
    {
        Object object = null;
        try
        {
            object = new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject();
        } catch (final Exception ioe)
        {
            ioe.printStackTrace();
        }
        return object;
    }

That takes the serialized bytes and supposed to return instance (of the pre-loaded class using my custom class loader) .. I got the following exception:

11/03/06 14:23:27 oracle.classloader.util.AnnotatedClassNotFoundException: 

      Missing class: mainSearchObjects.dc_index

    Dependent class: java.io.ObjectInputStream
             Loader: jre.bootstrap:1.5.0_06
        Code-Source: unknown
      Configuration: jre bootstrap

This load was initiated at MainSearch.web.MainSearch:0.0.0 using the Class.forName() method.

The missing class is not available from any code-source or loader in the system.
11/03/06 14:23:27   at oracle.classloader.PolicyClassLoader.handleClassNotFound (PolicyClassLoader.java:2068) [/D:/jdevstudio10134/j2ee/home/lib/pcl.jar (from system property java.class.path), by sun.misc.Launcher$AppClassLoader@14916158]
    at oracle.classloader.PolicyClassLoader.internalLoadClass (PolicyClassLoader.java:1679) [/D:/jdevstudio10134/j2ee/home/lib/pcl.jar (from system property java.class.path), by sun.misc.Launcher$AppClassLoader@14916158]
    at oracle.classloader.PolicyClassLoader.loadClass (PolicyClassLoader.java:1635) [/D:/jdevstudio10134/j2ee/home/lib/pcl.jar (from system property java.class.path), by sun.misc.Launcher$AppClassLoader@14916158]
    at oracle.classloader.PolicyClassLoader.loadClass (PolicyClassLoader.java:1620) [/D:/jdevstudio10134/j2ee/home/lib/pcl.jar (from system property java.class.path), by sun.misc.Launcher$AppClassLoader@14916158]
    at java.lang.ClassLoader.loadClassInternal (ClassLoader.java:319) [jre bootstrap, by jre.bootstrap:1.5.0_06]
    at java.lang.Class.forName0 (Native method) [unknown, by unknown]
    at java.lang.Class.forName (Class.java:242) [jre bootstrap, by jre.bootstrap:1.5.0_06]
    at java.io.ObjectInputStream.resolveClass (ObjectInputStream.java:574) [jre bootstrap, by jre.bootstrap:1.5.0_06]
    at java.io.ObjectInputStream.readNonProxyDesc (ObjectInputStream.java:1538) [jre bootstrap, by jre.bootstrap:1.5.0_06]
    at java.io.ObjectInputStream.readClassDesc (ObjectInputStream.java:1460) [jre bootstrap, by jre.bootstrap:1.5.0_06]
    at java.io.ObjectInputStream.readOrdinaryObject (ObjectInputStream.java:1693) [jre bootstrap, by jre.bootstrap:1.5.0_06]
    at java.io.ObjectInputStream.readObject0 (ObjectInputStream.java:1299) [jre bootstrap, by jre.bootstrap:1.5.0_06]
    at java.io.ObjectInputStream.readObject (ObjectInputStream.java:339) [jre bootstrap, by jre.bootstrap:1.5.0_06]
...........

I understood that.. The bootstrap class loader that is used by the de-serization code cannot see the class loaded by one of its children (my class loader) and I think this is a correct behaiour, isn't it?

So, Isn't a solution to this problem??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文