How to edit class file from a JAR file? We can call this process as patching a Java executable (Jar files) without knowing the source code. Let’s see the easy way of patching the Java executable file.

Methods

  1. Decompile the JAR file > Import decompiled classes to an IDE (Eclipse, NetBeans, etc.) > Modify source code > Recompile > Rebuild/Repack as JAR

  2. Extract Java classes from JAR> Modify Java Bytecode > Verify > Repack the JAR file

  3. Extract Java classes from executable > Modify Java Bytecode > Update the executable with modified classes/files using a command prompt

First method mentioned above is good option, if you have the source code and familiar with Java compilation, OO concepts but it has few complications mentioned below,

  1. The targeted JAR file has dependencies to other libraries. You should be familiar with those dependencies to your project
  2. The decompilation process is not an exact science, so expect to fix errors before getting it to recompile

When we just want is to edit the few lines, recompiling the whole project without compilation errors is a painful job. So method(1) is not mandatory one to use, lets see about the method(2). Method(2) is faster, less error-prone and quite simple to execute.

High-level steps:

  1. Setup the environment
  2. Use JD-GUI to peek into the JAR file
  3. Unpack the JAR file
  4. Modify the .class file with a Java Bytecode Editor
  5. Repack the modified classes into new archive file
  6. Verify it with JD-GUI

1. Setup the environment

Verify you have JDK installed and classpath have been made setup rightly, to verify -> Open Command Prompt

Type

java -version

In my machine, result is something like below,

java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)



2. Use JD-GUI to peek into the JAR file

I prefer to JD-GUI to browse the JAR file, you can install JD-GUI from below link,

http://jd.benow.ca/

While browsing the JAR file in our example, we could see below class in JD-GUI,

3. Unpack the JAR file

Create a new folder and copy the JAR file to the folder. Using below command will unpack the jar file in the folder.

jar -xf HelloWorld.jar

4. Modify the .class file with a Java Bytecode Editor

I prefer Java Bytecode Editor to edit the .class file. Java Bytecode Editor can downloaded from below link,

http://set.ee/jbe/

In this example, I’m going to change the string “Hello” to “Welcome” from HelloWorld.class file in bytecode editor. In order to edit, navigate to the respected package, file in JBE.




5. Repack the modified classes into new archive file

Navigate to HelloWorld folder and give following command in CMD prompt to repack the JAR file.

jar -cvf HelloWorld-Custom.jar *.*

6. Verify it with JD-GUI

Now, you would be aware of  browing the JAR file using JD-GUI. Open the newly created HelloWorld-Custom.jar file and verify the class file for the modification.

Now let’s see the method (3) which is my preferred method,

Extract Java classes from executable > Modify Java Bytecode > Update the executable with modified classes/files using a command prompt

In this method, only step 5 varies with method (2).

High-level steps:

  1. Setup the environment
  2. Use JD-GUI to peek into the JAR file
  3. Unpack the JAR file
  4. Modify the .class file with a Java Bytecode Editor
  5. Update the modified classes into existing JAR file
  6. Verify it with JD-GUI

Follow everything as in method (2) except step 5. Once step 4 is completed,

5. Update the modified classes into existing JAR file

Using following command in command prompt, updated .class file can be replaced into the existing JAR file. Now navigate to folder where JAR file is available in command prompt and give following command.

jar uf jar-file input-file(s)

In this command:

  • The u option indicates that you want to update an existing JAR file.
  • The f option indicates that the JAR file to update is specified on the command line.
  • jar-file is the existing JAR file that is to be updated.
  • input-file(s) is a space-delimited list of one or more files that you want to add to the JAR file.

Any files already in the archive having the same path name as a file being added will be overwritten.

So, in our example – I’m going to provide below command.

jar uf -HelloWorld.jar com/talksinfo/HelloWorld.class

Now the Jar file will be updated with new class file. For detailed information on updating jar file, you can refer below link,

https://docs.oracle.com/javase/tutorial/deployment/jar/update.html

Conclusion

Hence we saw how to edit class file from a JAR file. We discussed 3 methods and method (3) is easy way to achieve the patching of Jar file.

References

http://jd.benow.ca/

http://set.ee/jbe/

https://docs.oracle.com/javase/tutorial/deployment/jar/update.html

Please follow and like us: