开发者

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

开发者 https://www.devze.com 2023-03-18 22:49 出处:网络
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

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.)


You need a Java Development Kit (JDK). This includes a Java compiler (usually named javac), and an jar archiver.

Assuming you have the java files in a directory names src (then according to their package structure), you would use

javac -d classdir -sourcepath src src/*.java src/*/*.java src/*/*/*.java ...

to compile all files. (Adjust the number of * to the number of directory levels. If you only have some folders with source files in them, you can also list them individually. If some classes depend on others, you can ommit the others, the compiler will find and compile them automatically.)

If the program needs external libraries, provide these with a -classpath argument.

Now we have all compiled classes in the directory classdir. Look into your original jar file: any non-class files in there should also copied into your classdir (in the same relative directory they were before). This most notably includes META-INF/MANIFEST.MF.

Then we create a new jar file from these. The jar tool is included in the JDK.

jar cfm mypackage.jar classdir/META-INF/MANIFEST.MF -C classdir .

(You can also simply use a zip program of your confidence and rename the resulting zip file to .jar. If your files have non-ASCII names, make sure to set the filename encoding to UTF-8.)


In General, to compile Java Code into classes you need the javac executable that comes with the JDK.

Unix:

${JAVA_HOME}/bin/javac -d OUTPUT_DIRECTORY SOURCE_FILES

Windows:

%JAVA_HOME%\bin\javac.exe -d OUTPUT_DIRECTORY SOURCE_FILES

Once you've compiled the code, you can create the jar (which is a zip file that contains all the classes and some meta-data about the creator, version, ...etc).

${JAVA_HOME}/bin/jar cf output.jar INPUT_FILES

You can read more about the different options you can use with javac and jar. Using a tool like ant makes it easier to compile source code and create jars. See the javac and the jar tasks.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号