0
我想讀取存儲在我的jar文件中的類。從jar加載類時截斷的類文件異常
當我做這樣的
private void registerClasses(Path jar, List<String> classNames) throws IOException {
JarFile jarFile = new JarFile(jar.toFile());
Enumeration<JarEntry> e = jarFile.entries();
while (e.hasMoreElements()) {
JarEntry je = e.nextElement();
if (je.isDirectory() || !je.getName().endsWith(".class")) {
continue;
}
String className = je.getName().substring(0, je.getName().length() - 6).replace('/', '.');
if (classNames.contains(className)) {
LOGGER.info("Found class in jar [{}].", className);
InputStream stream = new JarInputStream(jarFile.getInputStream(je));
byte[] classBytes = IOUtils.toByteArray(stream); // empty array
Class<?> clz = this.defineClass(className, classBytes, 0, classBytes.length);
LOGGER.info("Defined class: [{}].", clz.getClass());
}
}
}
我得到java.lang.ClassFormatError: Truncated class file
和byteArray
顯示爲空。
什麼問題?
你確定.class文件不是空的嗎? – EJP
是的。我提供了由IDE構建的工件 – lapots