Shell Script to Validate JAVA Version
5:49 PM Posted by Preetish Panda
In this post we'll go through a shell script to validate JAVA version. In my recent project, I had to check for JVM version greater than or equal to 1.6.Lower version of JVM would throw UnsupportedClassVersionError when class compiled with higher version of compiler gets executed.My utility gets called from shell script.So, I put a put check in the script to validate minimum JDK version of 1.6.Given below is the shell script and it was tested successfully in Korn shell.
#Put value in required version. Example 1.4,1.5
REQUIRED_VERSION=1.6
#Converting the value in numeric value for comparison in later part of the script REQUIRED_VERSION=`echo $REQUIRED_VERSION | sed -e 's;\.;0;g'`
#Redirecting complete output of java -version to tmp.ver file
java -version >tmp.ver 2>&1
#Getting current version from the tmp.ver file
VERSION=`cat tmp.ver | grep "java version" | awk '{ print substr($3, 2, length($3)-2); }'`
rm tmp.ver
#Coverting into numeric value
VERSION=`echo $VERSION | awk '{ print substr($1, 1, 3); }' | sed -e 's;\.;0;g'`
if [ $VERSION ];then
if [ $VERSION -ge $REQUIRED_VERSION ];then
echo requirement matched
else
echo lower version
fi
else
echo not able to find java version
fi
REQUIRED_VERSION=1.6
#Converting the value in numeric value for comparison in later part of the script REQUIRED_VERSION=`echo $REQUIRED_VERSION | sed -e 's;\.;0;g'`
#Redirecting complete output of java -version to tmp.ver file
java -version >tmp.ver 2>&1
#Getting current version from the tmp.ver file
VERSION=`cat tmp.ver | grep "java version" | awk '{ print substr($3, 2, length($3)-2); }'`
rm tmp.ver
#Coverting into numeric value
VERSION=`echo $VERSION | awk '{ print substr($1, 1, 3); }' | sed -e 's;\.;0;g'`
if [ $VERSION ];then
if [ $VERSION -ge $REQUIRED_VERSION ];then
echo requirement matched
else
echo lower version
fi
else
echo not able to find java version
fi
Click here to Subscribe news feed from "TechFuzon", so that you do not miss out anything that can be valuable to you !!

















January 16, 2012 5:12 PM
This site helps the people related to java scripts field..