[Topic] Detect Java Installation with Shell Script

To assist in deploying AgileTrack in environments that already have a Java installation that may not be in the path, a simple shell script was created that searches the local drive for possible Java installations, and attempts to verify that the major version requirement is met.

The script first check for an existing JAVA_HOME directory and if one is found, tries to determine if the Java version meets the minimum requirement declared in the script. If that failes, it then uses the slocate database to do a quick search for Java executables, and checks the version of each one until an adequate version is found. Once a sufficient Java installation is found, the path to its Java home directory will be exported in JAVA_HOME for source scripting, and also echoed to standard out.

The script provided here was based off of the install-detect-java script provided with AgileTrack Linux distributions. The original script is used to set a configuration file that stores the path to the Java installation that is to be used. It also provides text feedback and instructions for the user.

#!/bin/bash
# This script attempts to find an existing installation of Java that meets a minimum version
# requirement on a Linux machine.  If it is successful, it will export a JAVA_HOME environment
# variable that can be used by another calling script.
#
# To specify the required version, set the REQUIRED_VERSION to the major version required, 
# e.g. 1.3, but not 1.3.1.
REQUIRED_VERSION=1.5

# Transform the required version string into a number that can be used in comparisons
REQUIRED_VERSION=`echo $REQUIRED_VERSION | sed -e 's;\.;0;g'`
# Check JAVA_HOME directory to see if Java version is adequate
if [ $JAVA_HOME ]
then
     JAVA_EXE=$JAVA_HOME/bin/java
     $JAVA_EXE -version 2> tmp.ver
     VERSION=`cat tmp.ver | grep "java version" | awk '{ print substr($3, 2, length($3)-2); }'`
     rm tmp.ver
     VERSION=`echo $VERSION | awk '{ print substr($1, 1, 3); }' | sed -e 's;\.;0;g'`
     if [ $VERSION ]
     then
          if [ $VERSION -ge $REQUIRED_VERSION ]
          then
               JAVA_HOME=`echo $JAVA_EXE | awk '{ print substr($1, 1, length($1)-9); }'`
          else
               JAVA_HOME=
          fi
     else
          JAVA_HOME=
     fi
fi

# If the existing JAVA_HOME directory is adequate, then leave it alone
# otherwise, use 'locate' to search for other possible java candidates and
# check their versions.
if [ $JAVA_HOME ]
then
     :
else
     for JAVA_EXE in `locate bin/java | grep java$ | xargs echo`
     do
          if [ $JAVA_HOME ] 
          then
               :
          else
               $JAVA_EXE -version 2> tmp.ver 1> /dev/null
               VERSION=`cat tmp.ver | grep "java version" | awk '{ print substr($3, 2, length($3)-2); }'`
               rm tmp.ver
               VERSION=`echo $VERSION | awk '{ print substr($1, 1, 3); }' | sed -e 's;\.;0;g'`
               if [ $VERSION ]
               then
                    if [ $VERSION -ge $REQUIRED_VERSION ]
                    then
                         JAVA_HOME=`echo $JAVA_EXE | awk '{ print substr($1, 1, length($1)-9); }'`
                    fi
               fi
          fi
     done
fi

# If the correct Java version is detected, then export the JAVA_HOME environment variable
if [ $JAVA_HOME ]
then
     export JAVA_HOME
     echo $JAVA_HOME
fi
—Posted by Adam Lane on Jul 30, 2007

Comments

6JdAO1 <a href="http://fjfdmuigpcop.com/">fjfdmuigpcop</a>, [url=http://mckqupriudpv.com/]mckqupriudpv[/url], [link=http://uiwlvzosdsgt.com/]uiwlvzosdsgt[/link], http://kgkoacwnkqnb.com/

—Posted by folcklord on Jul 19, 2010 at 10:19:11 AM

fiuVRy <a href="http://bqthoyhdmsav.com/">bqthoyhdmsav</a>, [url=http://hwfvzphjmmdt.com/]hwfvzphjmmdt[/url], [link=http://zozbufnwzroi.com/]zozbufnwzroi[/link], http://zbzrrvbkneut.com/

—Posted by lymanknap on Aug 6, 2010 at 3:33:47 PM


You may post a reply to this topic, but you must be logged in. If you already have an account, you may login now. If you need to create an account, you may also register now.


Blogs

Nov 6, 2008
"Impact of Virtualization in IT "

Jul 30, 2007
"Detect Java Installation with Shell Script"

Aug 6, 2007
"Software Developer Office Space"

May 5, 2008
"Coding Standards and Style"

Oct 6, 2007
"Telecommuting and Software Development"

Nov 6, 2008
"Evolution of Virtualization "

Jul 30, 2007
"Using Inno Setup to Create a Versioned Installer"