Add/Update a Class File in a Jar

Updating a class file directly in a jar comes very handy when you are working on a remote server and you need to update some classes in your deployed jar file. With update feature of the jar command, we can directly update the selective classes in the jar rather than building and moving the jar file itself to the remote server.

Let us assume we have abc.jar and we need to update the file Foo.class in the jar. Foo.class is in the package com.tm.example.

In order to update the jar we need to create a directory structure reflecting the package structure of the class which we need to replace and place the class there. In this case we need to place the class, Foo.class, in the directory com/tm/example. Also, let us keep the directory com is on the same level as that of abc.jar.

Now we can execute the following command to update the class file into the jar file.

        jar uf abc.jar com/tm/example/Foo.class

The above command works for both Linux and Windows.

Please note if we do not provide the directory structure and try to add the class like

        jar uf abc.jar Foo.class

this will add the Foo.class in the jar on the root level.

In case we need to add/update multiple files in jar, we can do it by separating the file name with spaces like:

        jar uf abc.jar com/tm/example/Foo.class com/rm/example/another/Bar.class

For more details visit this java tutorial.

Enjoy Learning,
Kamlesh

Advertisements