Friday, October 10, 2008

Searching Jar Files in Unix / Linux

Lot of times we get the following errors:
Exception in thread "main" java.lang.NoClassDefFoundError: server

And we just don't know which jar file is missing from the classpath. And we need to know the correct jar file name(s) to fix the problem. So, here I create a shell script that would allow you to search through all the jar files by specifying a specific keyword. You may modify this script to search in all files (*) or whatever your criteria may be. This script works recursively for all sub-folders as well. So, you can keep this script on the root level of search and simply execute it.

=====================================
searchjars.sh
=====================================


#!/bin/sh

if [ $# -ne 1 ];
then
echo "Usage: ./searchjars.sh <keyword>"
exit
fi

LOOK_FOR="$1"

for i in `find . -name "*jar"`
do
#echo "Looking in $i ..."
jar tvf $i | grep $LOOK_FOR > /dev/null
if [ $? == 0 ]
then
echo "==> Found \"$LOOK_FOR\" in $i"
fi
done


After saving this file, don't forget to give the execute permissions to this script.

[jboss@lin01 xlclient]$chmod +x searchjars.sh

Now you are ready to execute as follows:

If you don't specify any value, the script shows you the usage command:
[jboss@lin01 xlclient]$ ./searchjars.sh
Usage: ./searchjars.sh <keyword>

When you specify the value to be searched, you will see the files that have that value in it.
[jboss@lin01 xlclient]$ ./searchjars.sh server
==> Found "server" in ./java/lib/rt.jar
==> Found "server" in ./ext/jdbcpool-0.99.jar
==> Found "server" in ./ext/nexaweb-nfc-api.jar
==> Found "server" in ./ext/jai_core.jar
==> Found "server" in ./ext/javagroups-all.jar
==> Found "server" in ./ext/jbossall-client.jar
==> Found "server" in ./ext/jboss-client.jar
==> Found "server" in ./ext/nexaweb-common.jar
==> Found "server" in ./ext/soap.jar
==> Found "server" in ./lib/xlVO.jar
==> Found "server" in ./lib/XellerateClient.jar
[jboss@lin01 xlclient]$

If you need to see what files are being looked at, uncomment (remove the #) the following line in the script:
#echo "Looking in $i ..."

1 comment:

Unknown said...

I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post.

Bry
www.gofastek.com