This program searches through all .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.
Logic of script: The key is to get a list of all the jar files in present directory, open each jar archive and search it with our input classname, and for all positive output, print the name of the jar file on screen.
Sample Usage:
Note:
1. After downloading, rename the file to "JarSearch.sh". Transfer the JarSearch.sh file to Linux machine in ASCII mode to the location where you want to search. Give permission:
2. Before using the script, verify that the JAVA_HOME environment variable is set correctly on your system.
3. Information
About: Searches .jar in current path for a specified class file including sub-directories.
Usage: ./JarSearch.sh class_name
Example: ./JarSearch.sh Object
Caution: Give exact class name without .class extension or package inormation. This searches for file name.
Known issue: Can not be used to search inner classes due to $ sign in the name. You can fork the code.
Tested for JDK 1.5 and 1.6 on SUSE Linux in ksh, csh and bash shells.
Logic of script: The key is to get a list of all the jar files in present directory, open each jar archive and search it with our input classname, and for all positive output, print the name of the jar file on screen.
Sample Usage:
jar -tf rt.jar | grep "Object.class"
Note:
1. After downloading, rename the file to "JarSearch.sh". Transfer the JarSearch.sh file to Linux machine in ASCII mode to the location where you want to search. Give permission:
chmod -x JarSearch.shSometimes, before giving permission, it may be required to convert the file format using below command to UNIX format. This will not affect the functionality.
dos2unix JarSearch.sh
2. Before using the script, verify that the JAVA_HOME environment variable is set correctly on your system.
echo $JAVA_HOMEIf not set, set it to JDK installation directory.
3. Information
About: Searches .jar in current path for a specified class file including sub-directories.
Usage: ./JarSearch.sh class_name
Example: ./JarSearch.sh Object
Caution: Give exact class name without .class extension or package inormation. This searches for file name.
Known issue: Can not be used to search inner classes due to $ sign in the name. You can fork the code.
Tested for JDK 1.5 and 1.6 on SUSE Linux in ksh, csh and bash shells.
Download link: http://ideone.com/plain/W7xM03The script: JarSearch.sh
#!/bin/sh
className=$1
JAR=$JAVA_HOME/bin/jar
JARFILES=`find . -name "*jar"`
found=0
ask=1
#########################################
#Author: Rajdeep Biswas
#Created: 2014-04-30
#Website: www.javabambino.blogspot.com
#Known issue: Not for inner classes
#########################################
echo ""
echo "*****************************************"
echo "About: Searches .jar in current path (sub-directories inclusive) for a specified class file."
echo "Usage: $0 class_name"
echo "Example: $0 HashMap"
echo "Note: Give exact class name without .class extension."
echo "*****************************************"
echo ""
#Error if there is no or multiple arguments
if [ $# -ne 1 ]
then
echo "ERROR: Wrong number of arguments specified, please check usage above!"
exit 1;
fi
#Ask how to search
while [ $ask -eq 1 ]
do
echo "1. Enter '1' to search for exactly $className.class"
echo "2. Enter '2' to search for *$className.class pattern. For example, Sample$className.class"
echo "3. Enter '0' to exit this program"
read temp
#validate input is only 1, 2 or 0. If not, take input again.
if [[ -n ${temp//[0-2]/} ]];
then
echo -e "Caution: Invalid input!\n"
continue
fi
if [ $temp -eq 0 ]
then
echo "Exiting.."
exit 1
fi
if [ $temp -eq 1 -o $temp -eq 2 ]
then
searchHow=$temp
ask=0
fi
done
#Set the grep pattern accordingly as user input on how to search
if [ $searchHow -eq 1 ]
then
searchWhat="grep -w $className.class"
elif [ $searchHow -eq 2 ]
then
searchWhat="grep $className.class"
fi
echo -e "Scanning started...\n"
#Start scan
for JARFILE in $JARFILES
do
$JAR tvf $JARFILE | $searchWhat > /dev/null
if [ $? -eq 0 ]
then
found=1
echo "==> Found in $JARFILE"
fi
done
#End scan
if [ $found -eq 0 ]
then
echo "$className.class not found in current path: "`pwd`
fi
echo -e "\nScanning over."

No comments:
Post a Comment
Liked or hated the post? Leave your words of wisdom! Thank you :)