"jar -uvf" Command to Replace a File in a JAR

Q

What are "jar" commands to replace 1 class file in a JAR file? I need to modify one class file and put it back to the JAR.

✍: FYIcenter.com

A

If you have a large JAR file and just want to replace 1 class file with a modified version, you can follow these steps:

1. Get the source code of that 1 class file.

2. Make changes to the source code.

3. Recompile the source code.

4. Update the JAR file with the new class file from the compilation.

See the example session bellow:

\fyicenter>edit Hello.java
class Hello {
   public static void main(String[] a) {
      System.out.println("Hello new world!"); 	
   }
}

\fyicenter>"%java_home%\bin\javac" Hello.java

\fyicenter>"%java_home%\bin\jar" -uvf myNew.jar Hello.class
adding: Hello.class(in = 420) (out= 289)(deflated 31%)

\fyicenter>"%java_home%\bin\java" -cp myNew.jar Hello
Hello new world!

Back to FAQ for JDK JAR (Java ARchive) Tool.

2016-06-13, 1453👍, 0💬