Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jar

javac

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

like image 473
sandy Avatar asked Jul 06 '11 23:07

sandy


People also ask

How do you decompile a jar and edit it?

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.

How do I recompile a Java file?

Open a command prompt window and go to the directory where you saved the java program. Assume it's C:\. Type 'javac MyFirstJavaProgram. java' and press enter to compile your code.


1 Answers

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

like image 67
Paŭlo Ebermann Avatar answered Sep 21 '22 10:09

Paŭlo Ebermann