This question already has an answer here:
-
Is there a java classfile / bytecode editor to edit instructions? [closed]
4 answers
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.
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).
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
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).