我正在尝试从字节流编写类加载器,但出现此错误。我已经阅读了文档,但我很困惑,因为这不应该发生。这是我要加载的内容:
class LoadedClass {
public void out(String msg) {
System.out.println(msg);
}
}
它存储在字节数组中
这是我的类加载器代码:
package byteclassloader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class PrintClass extends ClassLoader {
private String LoadedClass = "cafebabe00000033001e0a0005001309001400150a001600170700180700190100063c696e69743e010003282956010004436f646501000f4c696e654e756d6265725461626c650100124c6f63616c5661726961626c655461626c650100047468697301000d4c4c6f61646564436c6173733b0100036f7574010015284c6a6176612f6c616e672f537472696e673b29560100036d73670100124c6a6176612f6c616e672f537472696e673b01000a536f7572636546696c650100104c6f61646564436c6173732e6a6176610c0006000707001a0c000d001b07001c0c001d000e01000b4c6f61646564436c6173730100106a6176612f6c616e672f4f626a6563740100106a6176612f6c616e672f53797374656d0100154c6a6176612f696f2f5072696e7453747265616d3b0100136a6176612f696f2f5072696e7453747265616d0100077072696e746c6e002000040005000000000002000000060007000100080000002f00010001000000052ab70001b100000002000900000006000100000001000a0000000c000100000005000b000c00000001000d000e00010008000000400002000200000008b200022bb60003b10000000200090000000a00020000000300070004000a00000016000200000008000b000c000000000008000f0010000100010011000000020012";
private String name = "LoadedClass";
public Class findClass() {
byte[] ba = hexStringToByteArray(LoadedClass);
return defineClass(name, ba, 0, ba.length);
}
private static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte data[] = new byte[len / 2];
for( int i = 0; i < len; i+= 2 ) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
}
public class ByteClassLoader {
public static void main(String[] args) {
try {
Class LoadedClass = new PrintClass().findClass().getClass();
Object inst = LoadedClass.newInstance();
String out = "The quick brown fox jumps over the gluttonous sloth sow.";
Method printStr = LoadedClass.getMethod("out", new Class[] { String.class });
printStr.invoke(inst, out);
}
catch( InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e ) {
System.out.println(e.getMessage());
}
}
}
它抛出这个异常:
无法调用newInstance()
java.lang.Class 的类