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]
...........
我明白了。去調用代碼使用的引導類加載器無法看到它的一個子類(我的類加載器)加載的類,我認爲這是一個正確的方法,不是它?
所以,是不是解決這個問題?
詳情:http://tech-tauk.blogspot.com/2010/05/thread-context-classlaoder-in.html – 2011-03-06 16:59:17