Solved

Unix Shell Script To Verify Java Version

Posted on 2012-03-12
4
2,394 Views
Last Modified: 2012-06-27
Hello,

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...
0
Comment
Question by:cgray1223
4 Comments
 
LVL 7

Expert Comment

by:micropc1
ID: 37713093
This is probably what you want for the version part...

http://notepad2.blogspot.com/2011/05/bash-script-to-check-java-version.html

#!/bin/bash
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."
else
    echo "Java version is lower than 1.6."
fi
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 37713973
Or this could make it more future proof (pass the script 16, 17, 18 etc)

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

Open in new window

0
 
LVL 3

Assisted Solution

by:jcgd
jcgd earned 100 total points
ID: 37715207
to verify java in the path:

#!/bin/bash
JAVA=$(which java)
if [ ! -x "${JAVA}" ]; then
   echo java not found
else
   echo java in directory  $JAVA
fi

(here add micropc1 or CEHJ comments)
0
 
LVL 7

Accepted Solution

by:
gudii9 earned 300 total points
ID: 37715272
Here is interesting explanation, link

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

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
    else        
        echo version is less than 1.6
    fi
fi



http://stackoverflow.com/questions/7334754/correct-way-to-check-java-version-from-bash-script
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java SE 8u111  Lot of stuff broke 11 68
check java version using powershell 13 81
going to wrong jsp page 2 19
Chrome and Firefox Java 5 23
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now