We do not need to download special tool to search for classes in jar files. This post explains how to do that without the use of any additional tool.
You need to have java sdk installed on your machine to use this tip. If you are a java developer, you might already have it on your computer.
Search for a class in all the jar files
You can use the below command to search for a class name in all the available jars in the current folder and the subfolders.
forfiles /S /M *.jar /C "cmd /c jar -tvf @file | findstr /C:"classname" && echo @path"
Example: Search for Logger.class
C:\>forfiles /S /M *.jar /C "cmd /c jar -tvf @file | findstr /C:"Logger.class" && echo @file" 2567 Thu Sep 14 02:03:58 IST 2006 org/apache/log4j/Logger.class 885 Thu Sep 14 02:04:00 IST 2006 org/apache/log4j/spi/RootLogger.class C:\jars\lib\log4j-1.2.14.jar" 687 Wed Apr 13 10:48:08 IST 2011 org/quartz/core/ErrorLogger.class C:\jars\lib\quartz-all-1.8.5.jar" 3267 Sat May 08 12:40:52 IST 2010 org/slf4j/helpers/NOPLogger.class 1375 Sat May 08 12:40:50 IST 2010 org/slf4j/Logger.class 455 Sat May 08 12:40:50 IST 2010 org/slf4j/spi/LocationAwareLogger.class C:\jars\lib\slf4j-api-1.6.0.jar"
If ‘jar’ command generates error, you may need to add the java binary files folder to PATH environment variable. Otherwise, you can specify the full path of ‘jar.exe’ while running jar command. You can find this file in C:\Program Files\java\jdk*\bin
(or) C:\Program Files(x86)\java\jdk*\bin
List the classes in a jar file
jar -tvf jarfile | findstr /C:".class"
Find all the jar files in a folder
To list all the jar files in the current folder and all its subfolders, we can use the below command.
dir /S /b *.jar
List all the jars installed on the system
You can run the below command to list all the jar files available on your computer.
dir c:\ d:\ /S /b | findstr /E /C:".jar"
This command looks for jar files in the system drives C: and D:
Great one !
Saved me a lot of work :)
Thanks,
Tal
Thanks a lot, worked like a charm
Very nice, exactly what I was searching for. Thanks!
Can you also add how to look for a symbol(variable names/method names) in the jar files to make the article complete? Sometimes, we need to find which jar/class has a particular symbol/method defined. This also helps if we need to check on a production system if a jar file has the new method added to the class.
Great command, Thx
Very Useful. It finds the class among any number of jars :). Thanks
very very helpful. Thanks
i’m getting error saying:
‘forfiles’ is not recognized as an internal or external command. operable program or batch file
Which windows version are you running?
Great !!
Saved My Time !!
jar -tvf | perl -lane ‘print if /someName\.class/’
The forfiles command only allows for one option (e.g.@file). When using the wildcard (*.jar) on a recursive search, it doesn’t show the directory path of the jar file that is being processed. As shown below, it shows me the files within the jar files but not the actual jar file name. How can you see the file path as well?
forfiles /S /M *.jar /C “cmd /c jar -tvf @file”
META-INF/MANIFEST.MF
oracle/iam/saks/dev/adapters/GetOUForUser.class
META-INF/MANIFEST.MF
oracle/iam/saks/dev/adapters/GetOUForUser.class
oracle/iam/saks/dev/adapters/OUPrePop.class