2

I'm new to batch script coding and a little stuck here. I've got a .sh file that checks if version of java installed is at least 1.8.0:

# Minimal version
MINIMAL_VERSION=1.8.0

# Check if Java is present and the minimal version requirement
_java=`type java | awk '{ print $ NF }'`
CURRENT_VERSION=`"$_java" -version 2>&1 | awk -F'"' '/version/ {print $2}'` #says 1.8.0_65
minimal_version=`echo $MINIMAL_VERSION | awk -F'.' '{ print $2 }'` #says 8
current_version=`echo $CURRENT_VERSION | awk -F'.' '{ print $2 }'` #says 8

if [ $current_version ]; then
  if [ $current_version -lt $minimal_version ]; then
    echo "Error: Java version is too low. At least Java >= ${MINIMAL_VERSION} needed.";
    exit 1;
  fi
    else
      echo "Not able to find Java executable or version. Please check your Java installation.";
      exit 1;
fi

It is pretty clear. I need to write a .bat file for Windows exactly same logic. So here's where I'm stuck, because I do not know windows analogs for awk.

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
Anatolii Stepaniuk
  • 2,585
  • 1
  • 18
  • 24

1 Answers1

4

Here you can see how you can get java version like integer which can numerically compared:

@echo off
:: uncomment the line bellow if the java.exe is not in the %PATH% 
::PATH %PATH%;%JAVA_HOME%\bin\

java -version 1>nul 2>nul || (
   echo no java installed
   exit /b 2
)
for /f tokens^=2-5^ delims^=.-_^" %%j in ('java -fullversion 2^>^&1') do set "jver=%%j%%k%%l%%m"

if %jver% LSS 18000 (
  echo java version is too low 
  echo at least 1.8 is needed
  exit /b 1
)
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 1
    This is not reliable because the number of digits in the Java version string depends on the number of digits in full version. For example, Java 17 is not passing this test for me because it comes out as 1702, which is less than 18000 – Cameron Apr 20 '22 at 20:12