I want to be able to edit bytecode and recompile into executable class files. I have no idea how to do this. I have tried decompiling with javap -c and -v, edit something, and change it back to my Class file, but I get an error "Error: Could not find or load main class Test.class". I would also like to generate java source from the bytecode. Any help? I want to do this myself without the use of outside programs. I want to do it myself if at all possible.
-
Why would you want to do this? Kind of defeats the purpose of using Java. Is it just one of these, "because I can" things? – MadConan Nov 04 '13 at 17:04
-
2@tycoon177: If that is your goal, you could write your own java compiler. – MadConan Nov 04 '13 at 17:12
3 Answers
The output from javap
is not suitable input for an assembler. If you want to disassemble and reassemble Java bytecode, you will need to do one of the following:
- Use third-party tools with a third-party assembler format.
- Write your own tools that (dis)assemble a third-party assembler format.
- Write your own tools that use your own assembler format.
I would take a look at Soot and Krakatau, both of which have full (dis)assembling capabilities. Soot supports a handful of intermediate representations for bytecode. Krakatau, I believe, uses a representation based on the popular Jasmin (though the tool itself is written in Python).

- 25,075
- 57
- 69
-
1It's based on Jasmin syntax but it extends it to provide more control over the classfile and fix some of the ambiguities in the original Jasmin syntax. – Antimony Nov 04 '13 at 19:25
The java platform (as in JDK) does not offer tools to compile byte code source to class files (it doesn't even really specify an assembler syntax in the JLS).
You can use bytecode like assembler with the help of a bytecode assembler. Take a look at Jasmin: http://jasmin.sourceforge.net/ (the syntax isn't exactly the same as that output by javap though).

- 19,919
- 4
- 36
- 70
Javassist (Java programming assistant) is a load-time reflective system for Java. It is a class library for editing bytecodes in Java; it enables Java programs to define a new class at runtime and to modify a class file before the JVM loads it. http://java-source.net/open-source/bytecode-libraries

- 9,899
- 23
- 86
- 141
-
I would do this, but I want to do it myself and not use prewritten tools. (Other than the necessary). I want to do the conversions from bytecode to class files to source myself if I can. – tycoon177 Nov 04 '13 at 17:05
-
you have to learn the Class file format, so read whole page carefully http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html. It is more likely that you have many problems coding parsing method of them. Therefore I strongly advice you getting out of this way until there is not special purpose. – Ahmet Karakaya Nov 04 '13 at 17:09