Patching JARs with the help of Eclipse

This post shows how to patch an existing Java Archive (JAR) to add new functionality to it or change its behavior. This for example can be helpful to add some logging for debugging purposes.

  • Open Eclipse
  • Import 'Plug-ins and Fragments' into Workspace
  • Under 'Import As' mark 'Projects with source folder'
  • Next
  • Chose the project you want to modify (Target platform)
  • Locate the source file in the workspace/project and modify it
  • Save changes
  • Now do a right-click on the file to figure out where it is located
  • Leave Eclipse and go to the command line and 'cd' into the Eclipse workspace
  • cd into 'bin' and execute the following command
$ jar uvf theJarFile.jar ./com/example/etc/ModifiedJava*

Now it is possible to see the new logging stuff or whatever you did to the Java class. Be aware that this might cause severe problems or side effects in case someone else is depending on this file. ;-)

The following snippet helps to find a particular class file within a big junk of JAR files. This might help if you want to scan a local Maven repository.

find . -name *.jar | while read jarName; do echo "JAR: " $jarName; jar tf $jarName; done | grep "JAR:\|TheJavaFileIAmLookingFor.class" --color=always

Date published:
Date modified: 2013-02-07
Keywords: Trench, Eclipse, JAR, Java, Patch