0

I have a project with a folder "input" that contains a few java files(e.g. test.java).This folder is not a source root and that's right. I only want to analyze the files in the input folder with my program. One target is it to get the bytecode from the files in the input folder and save it in a string. I read a lot about getting bytecodes from classes(java agents,bcel) but it's not very clear to me how I should start. Do you have any ideas and tips for me ?

Edit: I compiled my files and saved it into a byte array. I used javaassist library as mentioned by someone. My code is now:

    ClassPool pool = ClassPool.getDefault();
    CtClass cc = pool.get("Test");
    byte[] b = cc.toBytecode();

System.out.println(Arrays.toString(b));

As result I am getting:

[-54, -2, -70, -66, 0, 0, 0, 52, 0, 93, 10, 0, 25, 0, 52, 8, 0, 53, 7, 0,...,115]

How can I get from the byte array the "human readable" form of bytecode for example:

iconst_0      // 03
istore_0      // 3b
iinc 0, 1     // 84 00 01
iload_0       // 1a
iconst_2      // 05
imul          // 68
istore_0      // 3b
goto -7       // a7 ff f9

Anyone has some idea?

vizero
  • 33
  • 5
  • Do you want the actual byte code or it's human-readably, assembly-like form? – Ben Steffan Apr 29 '17 at 14:06
  • Maybe try https://jboss-javassist.github.io/javassist/tutorial/tutorial.html , first example. – Marcin Bukowiecki Apr 29 '17 at 14:07
  • I would like to save both forms. I am still unsure which one of them I will use later, so it would be clever to save for the beginning both forms. – vizero Apr 29 '17 at 14:21
  • 2
    If the files are `.java` files, you'd need to compile them to get the bytecode. – Kayaman Apr 29 '17 at 14:40
  • There are some possibilities to create the bytecode from `.java` files **at runtime**, using the `JavaCompiler` API. See [this question and answers](http://stackoverflow.com/questions/935175/convert-string-to-code) for some examples (the question refers to a "String", but the answers show the use of the `JavaCompiler` API that could be applied to `.java` files as well) – Marco13 Apr 29 '17 at 15:22
  • On the command line, just `javap -c Test` – Holger May 02 '17 at 13:28

1 Answers1

2

"bytecode" refers to compiled classfiles. It doesn't make sense to ask how to get bytecode from a .java file because there is no bytecode to get. You need to compile them with javac, Eclipse, or similar. Then you can just read the resulting .class files any way you want.

Edit: To convert the classfile into a human readable form, you need a disassembler. A limited disassembler called javap is included in the JDK, so the simplest approach would be to just use that. Alternatively, you can use the Krakatau disassembler, which supports a lot more advanced features than javap.

Antimony
  • 37,781
  • 10
  • 100
  • 107
  • If I compiled my file and save the bytecode in a byte array, how can I get the human readable form of bytecode? See my edit, there is an example. – vizero Apr 30 '17 at 12:13
  • @vizero I updated my answer. You need a disassembler, such as javap or Krakatau. – Antimony Apr 30 '17 at 16:07