Make writing a habit together! This is the 14th day of my participation in the “Gold Digging Day New Plan · April More Text Challenge” [click here for more details]

Jar package class name keyword

The script function

This script looks for a certain keyword in the package name and class name of the Jar package and highlights the matching Jar package name and path.

To solve the problem

More used to locate the Java. Lang. NoClassDefFoundError and Java. Lang. ClassNotFoundException problems, as well as the class version, repeated or conflict problems.

Script code

#! / bin/bash fields in the # # # to find the jar package, more used to locate the Java. Lang. NoClassDefFoundError and Java lang. ClassNotFoundException problems, as well as the class version, repeated or conflict problems. find . -name "*.jar" > /tmp/find_in_jar_temp while read line do if unzip -l $line | grep $1 &> /tmp/find_in_jar_temp_second then echo $line | sed 's#(.*)#\x1b[1; 31m\1\x1b[00m#' cat /tmp/find_in_jar_temp_second fi done < /tmp/find_in_jar_tempCopy the code

The command format

Find-jar-field keyword or class name path for example./find-jar-field TaskQueue./lib/Copy the code

Effect of the command

Find the specified field in the JAR package

The script function

This script performs a binary content lookup in the Jar package for the specified field

To solve the problem

You can often solve some amazing problems, such as some functions did not take effect online, some logs did not print, new functions are not displayed, you can use this command to find if there is a problem with the package.

Script code

#! If [$# -lt 2]; if [$# -lt 2]; then echo 'Usage : jargrep text path' exit 1; fi LOOK_FOR=$1 LOOK_FOR=`echo ${LOOK_FOR//.//}` folder=$2 echo "find '$LOOK_FOR' in $folder " for i in `find $2 -name "*jar"` do unzip -p $i | grep "$LOOK_FOR" > /dev/null if [ $? = 0 ] then echo "==> Found "$LOOK_FOR" in $i" fi doneCopy the code

The command format

Grep -jar Keyword or path of the class name For example,./grep-jar TaskQue./libCopy the code

Effect of the command

Check jar package conflict scripts

The script function

This script is used to identify conflicting Jar packages, find all Jar packages containing the same class in a single root directory, and determine the similarity of Jar packages based on the number of the same class.

To solve the problem

Function is often used in some online is not available or not work as expected, use this script analysis whether there are two versions of the class, and the old version of the class is the Java virtual machine loading, in fact, the JVM specification and no provision of the same class under the class path load order, realize the JVM specification implementation mechanism of the virtual machine also each are not identical, Therefore, there is no way to tell which version of the same class will be loaded first, so Jar package collisions are a nasty problem.

Script code

#! If [$# -eq 0]; if [$# -eq 0]; if [$# -eq 0]; then echo "please enter classpath dir" exit -1 fi if [ ! -d "$1" ]; then echo "not a directory" exit -2 fi tmpfile="/tmp/.cp$(date +%s)" tmphash="/tmp/.hash$(date +%s)" verbose="/tmp/cp-verbose.log" declare -a files=(`find "$1" -name "*.jar"`) for ((i=0; i < ${#files[@]}; i++)); do jarName=`basename ${files[$i]}` list=`unzip -l ${files[$i]} | awk -v fn=$jarName '/.class$/{print $NF,fn}'` size=`echo "$list" | wc -l` echo $jarName $size >> $tmphash echo "$list" done | sort | awk 'NF{ a[$1]++; m[$1]=m[$1]","$2}END{for(i in a) if(a[i] > 1) print i,substr(m[i],2) }' > $tmpfile awk '{print $2}' $tmpfile | awk -F','  '{i=1; for(; i<=NF; i++) for(j=i+1; j<=NF; j++) print $i,$j}' | sort | uniq -c | sort -nrk1 | while read line; do dup=${line%% *} jars=${line#* } jar1=${jars% *} jar2=${jars#* } len_jar1=`grep -F "$jar1" $tmphash | grep ^"$jar1" | Awk '{print $2}' ` len_jar2 = ` grep -f "$jar2" $tmphash | grep ^ "$jar2" | awk '{print $2}' ` # Modified by Robert 2017.4.9 #len=$(($len_jar1 > $len_jar2 ? $len_jar1 : $len_jar2)) len_jar1=`echo $len_jar1 | awk -F' ' '{print $1}'` len_jar2=`echo $len_jar2 | awk -F' ' '{print $1}'` if [ $len_jar1 -gt $len_jar2 ] then len=$len_jar1 else len=$len_jar2 fi per=$(echo "scale=2; $dup/$len" | bc -l) echo ${per/./} $dup $jar1 $jar2 done | sort -nr -k1 -k2 | awk 'NR==1{print "Similarity DuplicateClasses File1 File2"}{print "%"$0}'| column -t sort $tmpfile | awk '{print $1,"\n\t\t",$2}' > $verbose echo "See $verbose for more details." rm -f $tmpfile rm -f $tmphashCopy the code

The command format

./jar-conflict-detect keyword or class name path for example./jar-conflict-detect./Copy the code

Effect of the command