从 Jar 文件中的 Class 文件中读取字节
Read bytes from a Class file within a Jar file
我有一个 .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);
}
}
从某种意义上说,返回的字节数组不为空。
我有一个 .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);
}
}
从某种意义上说,返回的字节数组不为空。