Recompile to *.Jar After decompile and fix the code?

Viewed 39737

I have a myfile.jar file. I use jd-gui to decompile all *.class in a folder in myfile.jar. I get many *.java files in many folders

After fixing some code in some *.java, now I would like to recompile all *.java back to *.class, and pack the whole folder back to myfile.jar.

How do I do this?

(This is my first time playing with java code.)

3 Answers

Since I had trouble getting the .jar file completely recompiled and working with my application, I wanted to share here the steps that were missing in the previous answers.

In my specific case, to make things as easy as possible, I wanted to only modify one .java file in the result of the decompilation and update it directly in the .jar file.

I started from the answer from Paulo Ebermann, and I ended up with:

javac -source 8 -target 8 -d classdir -cp \
"\
path/to/dep1.jar; \
path/to/dep2.jar; \
path/to/myoriginal.jar \
" path/to/my/java/class.java

cd classdir
jar -uf path/to/myoriginal.jar path/to/my/java/class.class

Notes:

  • the path/to/myoriginal.jar which is in the path of the dependencies, this forces the java compiler to look for missing classses in the original .jar file rather than in the local decompiled code (which would require recompilation and more dependencies) and therefore I have intentionally left out the . path in the list of class paths
  • the -source 8 -target 8 which were required to force the class to compile for the appropriate version of the runtime machine.
  • the -uf which indicates that the jar tool should only update the file with the provided class
Related