java – 从Jar文件中的Class文件中读取字节
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 从Jar文件中的Class文件中读取字节,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2634字,纯文字阅读大概需要4分钟。
内容图文
![java – 从Jar文件中的Class文件中读取字节](/upload/InfoBanner/zyjiaocheng/795/257bfe37edad451da88f11e9d0dec38b.jpg)
我有一个.jar文件,其中包含.class文件和.java文件.我想将特定.class文件的内容加载为byte []数组.
static byte[] getBytes(String javaFileName, String jar) throws IOException {
try (JarFile jarFile = new JarFile(jar)) {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
// We are only interested in .class files spawned by compiling the argument file.
if (entry.getName().endsWith(".class") &&
entry.getName().contains(Files.getNameWithoutExtension(javaFileName))) {
try (InputStream inputStream = jarFile.getInputStream(entry)) {
return Preconditions.checkNotNull(ByteStreams.toByteArray(inputStream));
} catch (IOException ioException) {
System.out.println("Could not obtain class entry for " + entry.getName());
throw ioException;
}
}
}
}
}
但是,当我运行此代码时,会返回一个空字节数组(new byte [0]).关于我在这里做错了什么的想法?
编辑:这段代码工作正常!错误是问题的另一部分.为了知识而保留这里的问题无论如何:)
解决方法:
这对我有用:
public class Main {
public static void main(String[] args) {
try {
byte[] bytes = getBytes("MeasurePrinter.class", "~/BundleSensorAlarmLib.jar");
System.out.println(bytesToHex(bytes));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static byte[] getBytes(String javaFileName, String jar) throws IOException {
// ... inputs check omitted ...
try (JarFile jarFile = new JarFile(jar)) {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
// We are only interested in .class files spawned by compiling the argument file.
if (entry.getName().endsWith(".class") &&
entry.getName().contains(javaFileName.substring(0, javaFileName.lastIndexOf(".")))) {
try (InputStream inputStream = jarFile.getInputStream(entry)) {
return getBytes(inputStream);
} catch (IOException ioException) {
System.out.println("Could not obtain class entry for " + entry.getName());
throw ioException;
}
}
}
}
throw new IOException("File not found");
}
private static byte[] getBytes(InputStream is) throws IOException {
try (ByteArrayOutputStream os = new ByteArrayOutputStream();)
{
byte[] buffer = new byte[0xFFFF];
for (int len; (len = is.read(buffer)) != -1;)
os.write(buffer, 0, len);
os.flush();
return os.toByteArray();
}
}
private static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
}
从某种意义上说,返回的字节数组不为空.
内容总结
以上是互联网集市为您收集整理的java – 从Jar文件中的Class文件中读取字节全部内容,希望文章能够帮你解决java – 从Jar文件中的Class文件中读取字节所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。