Search This Blog

Bash script to check Java version

  • to check the java version:
    java -version 2>&1 | grep "java version" | awk '{print $3}' | tr -d \"
    

    or

    java -version 2>&1 | grep "java version" | awk '{print $3}' | sed -e "s/\"//g"
    
  • to check if the java version is greater than 1.5 (jdk 5), use the script below:
    #!/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 5 ]]; then
        echo "Java version is greater than 1.5."
    else
        echo "Java version is lower than 1.5."
    fi
    

No comments:

Post a Comment