Unix Shell Script To Verify Java Version


I need to write a Unix Shell Script (preferably shell agnostic, but bash is ok) to verify that the system executing the .sh script has java 1.6 or greater installed and on the system path.  

Thanks for the help...
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This is probably what you want for the version part...


VER=`java -version 2>&1 | grep "java version" | awk '{print $3}' | tr -d \" | awk '{split($0, array, ".")} END{print array[2]}'`
if [[ $VER ge 6 ]]; then
    echo "Java version is greater than 1.6."
    echo "Java version is lower than 1.6."
Or this could make it more future proof (pass the script 16, 17, 18 etc)

test $(java -version 2>&1 | grep 'java version' | egrep -o '[1-2]\.[6-9]' | tr -d '.') -ge ${min_version}

Open in new window

to verify java in the path:

JAVA=$(which java)
if [ ! -x "${JAVA}" ]; then
   echo java not found
   echo java in directory  $JAVA

(here add micropc1 or CEHJ comments)
Here is interesting explanation, link

if type -p java; then
    echo found java executable in PATH
elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]];  then
    echo found java executable in JAVA_HOME    
    echo "no java"

if [[ "$_java" ]]; then
    version=$("$_java" -version 2>&1 | awk -F '"' '/version/ {print $2}')
    echo version "$version"
    if [[ "$version" > "1.6" ]]; then
        echo version is more than 1.6
        echo version is less than 1.6


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.