我從網絡獲取字節碼。我將字節數組轉換爲類類Java的字節碼
package l2soft.utils;
public final class CustomClassLoader extends ClassLoader {
public static CustomClassLoader _instance;
public static CustomClassLoader getInstance() {
return _instance;
}
public void defineCustomClass(byte[] bytecode) {
Class<?> clazz = defineClass(null, bytecode, 0, bytecode.length);
resolveClass(clazz);
}
}
但是當應用程序啓動時,無法找到派生類。
The import test.Test1 cannot be resolved
(與收到類compilled)
注:我不知道類文件名。我並不需要申請一類,服務器本身發送
UPD:
package l2soft.utils;
import java.util.HashMap;
import java.util.Map;
public final class CustomClassLoader extends ClassLoader {
private Map<String, Class<?>> cache;
public static CustomClassLoader _instance;
public static CustomClassLoader getInstance() {
return _instance;
}
public CustomClassLoader(ClassLoader parent) {
super(ClassLoader.getSystemClassLoader());
_instance = this;
cache = new HashMap<String, Class<?>>();
}
public void defineCustomClass(byte[] bytecode) {
Class<?> clazz = defineClass(null, bytecode, 0, bytecode.length);
resolveClass(clazz);
cache.put(clazz.getName(), clazz);
}
@Override
public synchronized Class<?> findClass(String name) throws ClassNotFoundException {
Class<?> result = cache.get(name);
if(result == null)
super.findClass(name);
return result;
}
}
這是我的自定義類加載器。 SomeClass通過這個類加載器和tes.Test1加載。但我看到錯誤:import test.Test1無法解析。這CustomClassLoader設置爲默認加載器(-Djava system.loader = l2soft.utils.CustomClassLoader)
你是下載所有必需的課程還是隻下載一個課程? – MadProgrammer 2012-08-16 23:34:51
'-verbose:class'是你的朋友遇到麻煩。 – biziclop 2012-08-17 10:27:42