Read bytes from class file in jar file

I have a file .jar

with files .class

and .java

. I want to load the content of a specific file .class

as an array 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(SdkConstants.DOT_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;
        }
      }
    }
  }
}

      

However, when I run this code, an empty byte array ( new byte[0]

) is returned to me . Any ideas on what I might be doing wrong here?

EDIT: This code works just fine! The error was in the other part of the question. Keeping the question here for the sake of knowledge anyway :)

+3


source to share


1 answer


This works for me:

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);
    }
}

      



in the sense that the returned byte array is not empty.

+4


source







All Articles