ファイルとファイル.jarを含むファイルがあります。特定のファイルの内容を配列として読み込みたい。.class.java.classbyte[]
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]) が返されます。私がここで間違っているかもしれないことについてのアイデアはありますか?
編集:このコードは問題なく動作します! エラーは質問の別の部分にありました。とにかく知識のためにここに質問を置いておきます:)