2011-03-06 64 views
0

加载某些类我有以下情形:需要通过引导类加载器

我已经包含类的.class数据byte[](从文件系统加载)

而且我还有一个这个类的一些对象的以前序列化为其他流的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 [email protected]] 
    at oracle.classloader.PolicyClassLoader.internalLoadClass (PolicyClassLoader.java:1679) [/D:/jdevstudio10134/j2ee/home/lib/pcl.jar (from system property java.class.path), by [email protected]] 
    at oracle.classloader.PolicyClassLoader.loadClass (PolicyClassLoader.java:1635) [/D:/jdevstudio10134/j2ee/home/lib/pcl.jar (from system property java.class.path), by [email protected]] 
    at oracle.classloader.PolicyClassLoader.loadClass (PolicyClassLoader.java:1620) [/D:/jdevstudio10134/j2ee/home/lib/pcl.jar (from system property java.class.path), by [email protected]] 
    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] 
........... 

我明白了。去调用代码使用的引导类加载器无法看到它的一个子类(我的类加载器)加载的类,我认为这是一个正确的方法,不是它?

所以,是不是解决这个问题?

回答