Need assistance to decompile a jar file, edit the a class, and save as jar file

23,787

Solution 1

I use JD-GUI to decompile .jar's, this has an option to export all the decompiled classes. If the .jar you are decompiling has not been obfuscated etc this source should be re-compilable (otherwise you will need to go about it by bytecode editing).

Once you have extracted the classes, you need to ensure when you compile you compile with the -CP parameter pointing at .;MyJar.jar so that dependencies within the .jar can be found at compile.

After you have recompiled and have your new .class file/s, open the .jar with 7z and replace the old .class file/s with your new one. You can do this step with the command line jar tool but 7z makes it easier. Again if the jar was obfuscated and class files have identical names with only case sensitive differences you may need to use Linux to reinsert the .class file. (probably possible on Windows, but I've never looked into it too deeply).


I previously decompiled this Java program to add a commandline interface, you can probably find the link in the comments;

http://ninjacave.com/jarsplice

Solution 2

then there are lot of free utilities available like WinZip , 7-ZIP you can use them.

Before opening jar file just change the extension of jar to zip file and then extract that particular class file that you want to edit , then decompile it using any decompiler ,make the changes , compile it back and then finally put it back in the zip file.

Hope it helps.

Thanks

Share:
23,787
user2635337
Author by

user2635337

Updated on August 20, 2020

Comments

  • user2635337
    user2635337 over 3 years

    I have a jar file, and I need to edit a particular class and save it back into the jar file. I tried different decompilers, but I'm not able to save back to the jar.

    I tried Eclipse along with JADCLIPSE to decompile. By using that, I can see the decompiled class file, but I'm unable to edit it. Could anyone please help me to resolve this?

  • user2635337
    user2635337 almost 11 years
    Thank you. Now I'm half way done. Im trying to edit a perticular java file in configurator.jar. I did edit the file.
  • user2635337
    user2635337 almost 11 years
    And set the class path as d:\project\configurator.jar. Im trying to compile the java in windows using command prompt. But, getting "package does not exists error". I doubt that the java programs does not able to import this package. ( java code -- import de.tu_berlin.aio.cams.configuration.CamsLogger.LogOperator; ) and ( Error is - package CamsLogger doesnot exist ) . I have checked this again, and package is present in mentioned path. Have I done any mistakes while setting class path. Please help.
  • Robadob
    Robadob almost 11 years
    if your jar has any dependencies you will need to include them in the class path to.