Next Spaceship

Driving into future…

Modify Java .class in a .jar

| Comments

Sometimes I need to know what’s inside to figure out a best solution, like modifying a Java .class file in a .jar file. After searching google and stack overflow for a while, I found this question is little cared, and almost all information is not complete. So I wrap them up and make the whole process runnable.

Use luyten (or JD GUI if you don’t care its bugs) to decompile the jar, and save all files to folder srcdir

Modify a java file, then compile it to .class file.

cd srcdir
vi com/.../
cp some_folder/original.jar ./
javac -cp "original.jar" com/.../

The last command will generate a file named A.class in the same folder as

Use luyten to check if the file A.class is modified.

jar -uf original.jar com/.../A.class
mv original.jar modified.jar

This will generate a modified.jar. Use luyten to check again if it’s modified.

Note: If depends other external jars other than original.jar, Add them to classpath by

javac -cp "original.jar;lib/*" com/.../

It’s important to use lib/* rather than lib/*.jar because the later will not work.