title: Batch file to set java version without installing
body: <p>I saw many similar questions but didn't get help I needed. 
  My requirement is to find the Java version 8 from the system folders and call a jar file without installing Java 8. No need to set <code>JAVA_HOME</code> and there are multiple versions of Java available in the system folders.</p>
  
  <p>I only needed to check the Major version of Java for my requirement.</p>
  
  <p>I wrote something like </p>
  
  <pre><code>@echo off
  
  call "%~dp0..\bin\java.exe"
  
  d:
  cd JARFOLDER
  java -jar test.jar
  </code></pre>
  
  <p>But I dont know to check the major version of java. </p>
  
  <p><strong>Update</strong>:
  I used both the solutions given by @Mofi and used for my requirement as below and it is working fine.</p>
  
  <pre><code>@echo off
  rem Get path of latest installed Java directly from Windows registry.
  for /F "skip=1 tokens=1,2*" %%N in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\javaws.exe" /v Path 2^&gt;nul') do if /I "%%N" == "Path" set "JAVA_8_HOME=%%P" &amp; goto JavaPathFound
  
  rem Path of Java not found in registry, search for 32-bit Java in the default
  rem program files folders of 64-bit and 32-bit Windows and take first found.
  if "%ProgramFiles(x86)%" == "" goto Windows_x86
  for /R "%ProgramFiles(x86)%" %%I in (java*.exe) do set "JAVA_8_HOME=%%~dpI" &amp; goto JavaPathFound
  :Windows_x86
  for /R "%ProgramFiles%" %%I in (java*.exe) do set "JAVA_8_HOME=%%~dpI" &amp; goto JavaPathFound
  
  echo Error: Java binary directory not found.
  echo/
  pause
  goto :EOF
  
  :JavaPathFound
  if not "%JAVA_8_HOME:~-1%" == "\" set "JAVA_8_HOME=%JAVA_8_HOME%\"
      set "JAVA_8_HOME=%JAVA_8_HOME%java.exe"
  
  for /F "tokens=1-3" %%A in ('"%JAVA_8_HOME%" -version 2^&gt;^&amp;1') do (
      if /I "%%A %%B" == "java version" (
          set "JavaVersion=%%~C"
          goto EvaluateVersion
      )
  )
  echo Error: Failed to determine version of installed Java.
  goto :EOF
  
  :EvaluateVersion
  echo Java version is: %JavaVersion%
  for /F "tokens=2 delims=." %%I in ("%JavaVersion%") do set "jver=%%I"
  echo Main version is: %jver%
  echo Path is: %JAVA_8_HOME%
  if %jver%==8 (
  echo "The processor will be started with Java 8"
  cd /D D:\JARFOLDER
  "%JAVA_8_HOME%" -jar test.jar
  )
  endlocal
  </code></pre>
  
  <p>Thanks in Advance.</p>