2

I have this java code

public class HelloWorld
{
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

I compile to class file, run it , and all fine.

I want to edit it via class file.

I open the HelloWorld.class with recaf.0.9, click on recompile, than I edit the code and save it, but the code not changes.

How can I edit .class file and not recompile it , and not working with bytecode.

I want for example add if else to this code(via the class file)

1 Answer 1

3

How can I edit .class file and not recompile it , and not working with bytecode.

These requirements are contradictory. You have to choose one or the other.

There are two main ways to edit Java code. You can either decompile the classfile, edit the decompiled source, and then try to compile it again, or you can edit it directly at the bytecode level using a bytecode disassembler and assembler such as Krakatau or a bytecode editor such as Recaf.

Decompilation and recompilation is unreliable, since the process is lossy in both directions, so it will generally only work in simple cases. You're better off editing the bytecode directly, since this is faster and guaranteed to work for all classfiles, but it does require you to understand bytecode. Note that Recaf is also lossy (unlike Krakatau), although it won't matter unless you're doing something highly unusual.

10
  • 1)why Decompilation and recompilation is lossy2) if I want to edit directly the bytecode, how can I check if I done it well ? wach it on JD-GUI? 3) by the way, if I edit 1 function from big class via bytecode, but edit only the internal logic of this functon, is that influence on another functon? thank you!
    – cdcsccd
    Feb 13, 2018 at 4:57
  • If you only edit the bytecode of one method, that won't affect any other methods. As for how to check your results, you could just disassemble it again.
    – Antimony
    Feb 13, 2018 at 15:07
  • I tried to edit bytecode (.class file) with cs.ioc.ee/~ando/jbe .I have class file with some functions , I tried to edit 1 func that get some arguments and have some local var .When I put this class file on intellij I see the name of arguments and name of local var . but when I click on save on JBE , the name of all arguments and local var changed to var1,var2 . why? I want to edit byte code without delete anything
    – cdcsccd
    Feb 13, 2018 at 16:15
  • Most likely, the tool you used didn't preserve the metadata attributes. Have you tried using Krakatau? That will preserve everything by default.
    – Antimony
    Feb 14, 2018 at 16:33
  • I don't understand how to use Krakatau , Is there any gui to use it on windows? I only want to edit bytecode, I dont want to re-compaile code and can you tell me pleaase how can I only edit byte code of 1 function from .class file that include few funcrion (I don't want touch another functions) without re-compaile it and saving on metadata? ot o you know another tool with gui for that ? thanks!
    – cdcsccd
    Feb 14, 2018 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.