Codebox Software

JAR Search Script

Published:

Searches through all Java .jar files in the current directory, and in any sub-directories, looking for the class that you specify. This can be very handy if you need to use a class in a Java program, but aren't sure which .jar file contains it.

Windows Version  Windows Logo

@echo off
set JAR=%JAVA_HOME%\bin\jar.exe
set USAGE="Usage: %0 <class name>"
if {%1}=={} (
  echo %USAGE% 
  goto EXIT
)

for /R %%i IN ("*.jar") do (
    "%JAR%" -tvf %%i | findstr "\/%1.class" && echo %%i && echo ----------------
) 
:EXIT

UNIX/Linux Version  Linux Logo

#!/bin/sh
JAR=$JAVA_HOME/bin/jar
USAGE="Usage: $0 class_name"
if [ $# -ne 1 ]; then
  echo $USAGE
  exit 1
fi
for JARFILE in $(find . -name *.jar); do
    $JAR -tf $JARFILE | grep "/$1.class" && echo $JARFILE && echo ----------------
done

Notes

Before using the script, verify that the JAVA_HOME environment variable is set correctly on your system (you could do this by just running the command %JAVA_HOME%\bin\jar.exe in Windows, or $JAVA_HOME/bin/jar on UNIX/Linux, and seeing whether you get the usage information for the jar utility, or just an error message). If the environment variable is not set you will have to manually edit the second line of the script.

Run the script with the name of the class that you are searching for as a command line parameter, the search is case-sensitive and you should specify the name of the class only, without the package name or the '.class' extension, for example:

JarSearch SAXParserImpl