How to easily edit a .class file

1.3k Views Asked by At

I have a .jar file WHICH I DID NOT CREATE with many class files inside. I need to edit one specific class file. I have jd-gui. The unedited code of this class file, as shown by JD is:

package net;

import java.util.UUID;

public class a1b {
  XF a = aTt.e();

@aez
  public int getFoodLevel() {
    return aFO.q(this.a.aa).b();
  }

@aez
  public void setSlot(int paramInt) {
    this.a.aa.ba.b = paramInt;
  }
}

what I would like to change it to is:

package net;

import java.util.UUID;

public class a1b {
  XF a = aTt.e();

@aez
  public int getFoodLevel() {
    return aFO.q(this.a.aa).b();
  }

@aez
  public void setSlot(int paramInt) {
    this.a.aa.ba.b = paramInt;
  }

@aez
  public int getSlot() {
    return this.a.aa.ba.b;
  }
}

so i just want to add a simple function that returns the value of "this.a.aa.ba.b" (the player slot in the game this class file is in) (the jar file is obfuscated)

i am very new to java, i've tried a few different ways including recaf, however recaf doesnt work as the file is obfuscated and fails with "compilation error".

this compilation error does not occur due to my added function, instead there are a few other functions in there which i did not include as they aren't related to this question which stop it from compiling. when i delete the offending functions it compiles.

1

There are 1 best solutions below

5
Stephen C On

You are trying to edit an obfuscated JAR file.

Obfuscated JAR files are created by the vendor with the express intention of preventing you from modifying the code. And one of the things that a good code obfuscator will do is to transform the bytecodes in a way that makes it difficult for a decompilation tool (such as the one you are using) to produce valid (i.e. compilable) Java source code.

That is what has happened here. The decompiler you are using has produced invalid Java source code.

What can you do?

  • You could try a different decompiler.

  • You could try reading the bytecodes yourself to work out what the correct code should be (for each of the errors) and then fix the source code. (But that will be hard for a novice Java programmer.)

  • You could ask the vendor of the software to provide you with a copy of the (real) source code.

  • You could just stop. If you read the license for the software you are trying to modify, you will probably find that it forbids decompilation, reverse engineering, and so on.


Note that since what you are doing could well be illegal, we would be ill-advised to assist you. It would put us (StackExchange Inc, and the people who assisted you) in a legally awkward position.