Shell Script to Validate JAVA Version

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
#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
                 echo lower version
         echo not able to find java version

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *