Saturday Aug 31

How to find the jar file containing a class definition ?

PDF Print E-mail
Friday, 07 September 2012 14:52
AddThis Social Bookmark Button


Sometimes you are looking for a class in an application server but don't know where this class is located. In Websphere common libraries have been repackaged by IBM using an in house naming convention.

Unix systems offers several useful commands to retrieve all files packaging a class without having to unzip all libraries.

The following snippet list the jar file containing the class " MyClass " :

find . -name '*.jar' | while read F; do (echo $F; jar -tvf $F | grep 'MyClass.class') done 

If you want to write the resut in a file you can use the following command

find . -name '*.jar' | while read F; do (echo $F; jar -tvf $F | grep 'MyClass.class') done >result.txt

Create a script

If you prefer to use a script you can define your own script to do the job

Create a shell script named


if [ -z "$2" ]
  echo Usage: $0 Directory Class Name
  exit 1

for f in $(find $1 -name '*.jar')
  jar tf $f | grep "$2" && echo "[in $f]"

If you need more informations about special variables you can click the link.

You can then find your class in the current folder using the command

findClass . MyClass
Tags: find , class , system , file , packaging , snippet , retrieve , list , posix

Add comment

Security code

Java Tutorial on Facebook