Saturday Aug 31

Key Concepts

How to find the jar file containing a class definition ?

PDFPrintE-mail
Friday, 07 September 2012 14:52
AddThis Social Bookmark Button

unix_logo

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 findClass.sh

#!/bin/sh

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

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

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
Refresh

Java Tutorial on Facebook