Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to check Java version from BASH script

How can I check whether Java is available (in the PATH or via JAVA_HOME) from a bash script and make sure the version is at least 1.5?

like image 339
Aaron Digulla Avatar asked Sep 07 '11 13:09

Aaron Digulla


People also ask

How do you identify the Java version in which the code is written?

If you want to know which version of Java was used to compile the class, you can use javap , available in the JRE and JDK. It reads your class bytecode and tells you the version. javap -v out/com/kineolyan/tzio/diffs/Main.

How do I determine bash script version?

To find my bash version, run any one of the following command: Get the version of bash I am running, type: echo "${BASH_VERSION}" Check my bash version on Linux by running: bash --version. To display bash shell version press Ctrl + x Ctrl + v.

How do I check if Java is installed in terminal?

1. Open command prompt and enter “java –version”. If installed version number is displayed.


1 Answers

Perhaps something like:

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.5" ]]; then         echo version is more than 1.5     else                  echo version is less than 1.5     fi fi 
like image 193
glenn jackman Avatar answered Sep 28 '22 15:09

glenn jackman