0

I'm trying to use Krakatau to assemble a native Java bytecode, acquired with javap -c, but I'm getting a weird error:

> python Krakatau/assemble.py Main.bc
Krakatau  Copyright (C) 2012-17  Robert Grosse
This program is provided as open source under the GNU General Public License.
See LICENSE.TXT for more details.

Processing file Main.bc, 1/1 remaining
Main.bc:1:1: error: Expected '.class' or '.version'.
Compiled from "Main.java"

I have checked everything on the project's GitHub repository already, but nothing seems to help it. How is it expecting a .class file when I'm using the assembler? Should my bytecode be in some form of Jasmin syntax?

joaofbsm
  • 625
  • 5
  • 13
  • Nothing in the Krakatua says that it is designed to convert the output of `java -p`. – Stephen C Nov 11 '17 at 04:15
  • This [post](https://stackoverflow.com/questions/19772897/how-can-i-reassemble-java-bytecode-generated-by-javap), which you have linked me (by marking my previous question as a duplicate), says exactly so. – joaofbsm Nov 11 '17 at 04:19
  • No it doesn't. Read that (linked) Q&As again. The answers say that you can't convert `javap -c` output back to bytecodes, but you can convert **other things** back to bytecodes. – Stephen C Nov 11 '17 at 04:21
  • I have read it. It does say so. – joaofbsm Nov 11 '17 at 04:26
  • Fine ... whatever. So make it work. But, basically, this information is all in the Q that I linked to and the respective tools' documentation. – Stephen C Nov 11 '17 at 04:28
  • @StephenC `javap -c` output IS explicit bytecode. – joaofbsm Nov 11 '17 at 04:32
  • 1
    Technically, no it isn't. It is a human readable rendering of bytecodes. Real bytecodes are .... expressed as binary data. – Stephen C Nov 11 '17 at 04:34
  • That is why I said it was explicit. Anyway, thanks for your time, the question is solved. – joaofbsm Nov 11 '17 at 04:35
  • 1
    You must be using the word "explicit" in some way that I (a native English speaker and IT professional with ~40 years experience) have never come across. – Stephen C Nov 11 '17 at 04:36

2 Answers2

1

Nothing in the Krakatau documentation says that it is designed to convert the output of javap -c.

If you want to use Krakatau to convert bytecode files, you should use Krakatau for the disassembly step not javap -c.

The Krakatua README.txt file explains how to do that.

Antimony
  • 37,781
  • 10
  • 100
  • 107
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Krakatau have 3 main tools: A decompiler, a disassembler and an assembler. To the extent of my knowledge, the disassembler works pretty similar to the `javap` command, but generating a Jasmin(**.j**) file, instead of bytecode. Assembling should do exactly the opposite. – joaofbsm Nov 11 '17 at 04:26
  • Yes. That is correct. But that doesn't mean that Krakatua understands `javap -c` output. The format will be different. – Stephen C Nov 11 '17 at 04:26
  • You are correct. It doesn't accept pure Java bytecode as an input, as I have stated in my answer to the question. – joaofbsm Nov 11 '17 at 04:31
1

The output of javap cannot be reassembled. It's designed to help Java programmers to debug their code and isn't complete or machine readable.

Krakatau uses an assembly format based on Jasmin syntax. Krakatau contains both an assembler and a disassembler, so you can use the Krakatau disassembler to disassemble a classfile into a textual assembly file, and then reassemble it into a classfile.

On a side note, javap is missing a lot of features and hides things from the output. It's useful for a quick check, but if you really want to see what is in a classfile at a low level, you need to use Krakatau anyway.

Antimony
  • 37,781
  • 10
  • 100
  • 107
  • Thanks @Antimony, but, as you can see [here](https://stackoverflow.com/questions/47215612/how-to-translate-three-address-codetac-to-java-bytecode), I can't disassemble my files using Krakatau because I'm translating them directly from Three Address Code. Just out of curiosity, is there any information loss in the `javap` process that makes going back impossible? – joaofbsm Nov 11 '17 at 12:53
  • 1
    @Joao, yes, `javap` loses tons of information, which is why I recommended avoiding it. The Krakatau disassembler does not lose any information. In fact, you can disassemble a classfile, reassemble it, and get back the exact same classfile you started with. Anyway, it sounds like you're generating an assembly file directly. All you need to do is modify your code to generate it in the Krakatau format, and you'll be able to assemble it. – Antimony Nov 11 '17 at 16:26