我的项目中有一个定制的ClassLoader,它加载一个在运行时从用户输入动态生成的类。构建它时,我通常遵循本教程的说明,但使用URLClassLoader作为父类,因为类在运行时可能会发生变化。
它可以很好地加载类本身,但是一旦尝试加载相关类,它就会在下面的代码中在第4行( NullPointerException )抛出一个stream.available(),因为返回的流为null。
//file is "java\lang\Object.class" when throwing the exception
private byte[] loadClassFileData(String file) throws IOException {
InputStream stream = parent.getResourceAsStream(file);
int size = stream.available();
byte buff[] = new byte[size];
DataInputStream in = new DataInputStream(stream);
in.readFully(buff);
return buff;
}对getResourceAsStream()使用默认的类加载器似乎没有帮助。
发布于 2022-07-13 04:19:51
https://stackoverflow.com/questions/50988090
复制相似问题