private byte[] getClassData(String name) {
try {
name = name.replace('.', '/');
name = name + ".class";
System.out.println("类路径为: " + name);
byte[] buffer = new byte[1024];
JarFile jarFile = new JarFile("target.jar");
Enumeration<?> enu = jarFile.entries();
while (enu.hasMoreElements()) {
JarEntry jarEntry = (JarEntry) enu.nextElement();
if (jarEntry.getName().equals(name)) {
InputStream inputStream = jarFile.getInputStream(jarEntry);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int read;
while ((read = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, read);
}
return byteArrayOutputStream.toByteArray();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return new byte[]{};
}
从JAR包中读取指定类的字节码
最新推荐文章于 2022-06-06 17:49:31 发布