1

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 JAVA_HOME and there are multiple versions of Java available in the system folders.

I only needed to check the Major version of Java for my requirement.

I wrote something like

@echo off

call "%~dp0..\bin\java.exe"

d:
cd JARFOLDER
java -jar test.jar

But I dont know to check the major version of java.

Update: I used both the solutions given by @Mofi and used for my requirement as below and it is working fine.

@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^>nul') do if /I "%%N" == "Path" set "JAVA_8_HOME=%%P" & 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" & goto JavaPathFound
:Windows_x86
for /R "%ProgramFiles%" %%I in (java*.exe) do set "JAVA_8_HOME=%%~dpI" & 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^>^&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

Thanks in Advance.

DeeJay007
  • 469
  • 4
  • 30
  • 2
    What do you mean with `system folders`? Oracle Java is not pre-installed with Windows. It must be separately installed by a user by downloading the installer and executing it. On none of my computers is Java installed as I don't need it whether for scripted tasks nor for web browsing. So your question is unclear for me. Please edit it and make it more clear. You hopefully know that each user has the possibility to install Java everywhere instead of default location during installation. And show us also the code you already have as Stack Overflow is not a free code writing service. – Mofi Jul 25 '17 at 05:35
  • @Mofi, by quoting not installing it means I need not have to set the JAVA_HOME and is not available in environment variables. I only needed to check the location of java.exe from installed path for version 8. – DeeJay007 Jul 25 '17 at 05:45
  • You don't have `JAVA` installed but `JAVA` or `JRE` folders are present in your `SYSTEM` folder. Is this what you mean? If yes, this `SYSTEM` folder has many `JAVA` folders inside them and you want to check the higher version of `JAVA`. Right? – Ashraf.Shk786 Jul 25 '17 at 05:59
  • 1
    See third batch code posted in [my answer](https://stackoverflow.com/a/44542924/3074564) on a similar question containing the code to find installation directory of latest installed Java. You can add additional code to check if latest installed Java is of version 1.8.x. The code for this check can be also found on Stack Overflow, for example [here](https://stackoverflow.com/a/41933395/3074564). – Mofi Jul 25 '17 at 06:05
  • @Ashraf.Shk786, Java is installed alright. YES, the System folders has many java versions and I want to find Java 8 by checking major version. – DeeJay007 Jul 25 '17 at 06:06
  • @DeeJay007 I've added an answer please check and let me know whether helpful – Ashraf.Shk786 Jul 25 '17 at 06:35

1 Answers1

0

Yes, you can do so by reading the release file present inside JRE folder of each of your JAVA folder inside SYSTEMS folder. It resides in directory C:\Program Files\Java\jre7\ so in your case it may be somewhat like this ...\SYSTEMS\Java\jre. Your script should be capable of reading this file from each of your JAVA or JRE folders and then try to find the biggest value.

Ashraf.Shk786
  • 618
  • 1
  • 11
  • 23