DEV Community

Cover image for What’s there in a Jar?
Ved Sharma
Ved Sharma

Posted on

What’s there in a Jar?

Many a times when we bundle an application in form of Jar, we need to checkout what is there in it. This is especially true when something goes wrong or application fails to startup in production or controlled environments where you don’t have access to UI tools. That is when this Jar utility come handy.

We will discuss how to use this utility to view, extract, re-bundle and update the existing resources added to the application.

Lets say your build system is creating a jar name connector-utility.jar and you want to list down all the content of this jar bundle, all you have to do is
You will see something like this

jar -tvf connector-utility.jar
0 Thu Jun 30 16:15:04 IST 2022 META-INF/
476 Thu Jun 30 16:15:04 IST 2022 META-INF/MANIFEST.MF
0 Fri Feb 01 00:00:00 IST 1980 org/
0 Fri Feb 01 00:00:00 IST 1980 org/springframework/
0 Fri Feb 01 00:00:00 IST 1980 org/springframework/boot/
0 Fri Feb 01 00:00:00 IST 1980 org/springframework/boot/loader/
5871 Fri Feb 01 00:00:00 IST 1980 org/springframework/boot/loader/ClassPathIndexFile.class
6806 Fri Feb 01 00:00:00 IST 1980 org/springframework/boot/loader/ExecutableArchiveLauncher.class
3966 Fri Feb 01 00:00:00 IST 1980 org/springframework/boot/loader/JarLauncher.class
1483 Fri Feb 01 00:00:00 IST 1980 org/springframework/boot/loader/LaunchedURLClassLoader$DefinePackageCallType.class
1535 Fri Feb 01 00:00:00 IST 1980 org/springframework/boot/loader/LaunchedURLClassLoader$UseFastConnectionExceptionsEnumeration.class
11154 Fri Feb 01 00:00:00 IST 1980 org/springframework/boot/loader/LaunchedURLClassLoader.class
5932 Fri Feb 01 00:00:00 IST 1980 org/springframework/boot/loader/Launcher.class
1536 Fri Feb 01 00:00:00 IST 1980 org/springframework/boot/loader/MainMethodRunner.class

Now lets say you want to replace one the files from the above listing with new file, it can be any file including properties file/Meta-Inf file. All you have to do is run this command. You can pass multiple files as well in one go to update as well.

jar -uvf connector-utility.jar ./org/springframework/boot/loader/MainMethodRunner.class

Lets say you want to extract entire jar and see the content for debugging/replacement purpose. You can use this command.

java -xvf connector-utility.jar

Lets say you updated or remove all the stuff that is needed and bundle it again, you can use below command. This will allow jar utility to go out directory in current path and bundle all the files under it with name connector-utility.jar.

jar -cvf connector-utility.jar -C out/ .

Top comments (0)