Unabled to determine java version when running .bat command from within Qt installer

拜拜、爱过 提交于 2020-03-03 12:14:05

问题


I would like to detect if java has been installed on a machine, and if so which JRE version. I have a simple .bat file script that runs and queries the registry. This works okay when run alone but when run within the Qt installer the same command returns no key, value pairs. I am a bit stumped. I have tried elevating the installer when executing the command, but frankly the script succeeds when running without privileges from the command, and it also did not make a difference.

For instance, when running a simplified version of the command:

for /f "tokens=*" %%j in (
   'reg query "HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"'
) do ( echo %%j )

When running the standalone script the registry values are printed out but when the same file is invoke (it is not compiled in to the installer but called via run-time) the same command above returns nothing.

I am using Qt installer framework 3.0.5.

The script is located in: com.domain.packagex\meta\myscript.bat

And is called from the package component qs script:

var returnArray = 
installer.execute(".\\packages\\com.domain.packagex\\meta\\myscript.bat");
...

From the printouts when run from the installer installer I can see that the script is being run and that is the latest version but the printouts look different (query command returns nothing). I have checked that the error level remains the same before and after the registry query.

I do not see this problem on my own Windows 10 (x64) machine but I do see it on a Virtualbox Windows 10 (x64) machine..


回答1:


I suppose Qt installer is a 32-bit application. Therefore 32-bit cmd.exe is started which runs 32-bit reg.exe on 64-bit Windows. So it is necessary to have knowledge about:

  • WOW64 Implementation Details
  • File System Redirector
  • Registry Keys Affected by WOW64

And it is also advisable in general to have knowledge about application registration on Windows.

This is a quick coded solution for getting current version of Java:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Use reg.exe in Windows system directory on 32-bit Windows
rem and on 64-bit Windows on being executed in 64-bit environment.
set "RegExe=%SystemRoot%\System32\reg.exe"

rem But on 64-bit Windows with running this batch file in 32-bit environment,
rem use system redirector Sysnative to run also 64-bit REG in the directory
rem %SystemRoot%\System32 instead of 32-bit REG in %SystemRoot%\SysWOW64.
if exist %SystemRoot%\Sysnative\reg.exe set "RegExe=%SystemRoot%\Sysnative\reg.exe"

rem First query the version of 64-bit Java on 64-bit Windows
rem respectively version of 32-bit Java on 32-bit Windows.
for /F "skip=2 tokens=1,2*" %%A in ('%RegExe% query "HKLM\SOFTWARE\JavaSoft\Java Runtime Environment" /v CurrentVersion 2^>nul') do if /I "%%A" == "CurrentVersion" set "JavaVersion=%%C" & goto HaveJavaVersion

rem Java version not found. On 64-bit Windows query version of 32-bit Java.
if not "%ProgramFiles(x86)%" == "" for /F "skip=2 tokens=1,2*" %%A in ('%RegExe% query "HKLM\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment" /v CurrentVersion 2^>nul') do if /I "%%A" == "CurrentVersion" set "JavaVersion=%%C" & goto HaveJavaVersion

rem Is Java installed and registered at all?
for /F "skip=2 tokens=1,2*" %%A in ('%RegExe% query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\javaws.exe" /v Path 2^>nul') do if /I "%%A" == "Path" set "JavaPath=%%C" & goto HaveJavaPath
goto JavaNotInstalled

:HaveJavaPath
if not exist "%JavaPath%\java.exe" goto JavaNotInstalled
rem Java prints version information to STDERR instead of STDOUT. For that
rem reason 2>&1 is necessary to redirect the output to STDOUT for being
rem captured by FOR from background command process started with cmd /C
rem by FOR for executing the command line within the round brackets.
for /F "tokens=2*" %%A in ('"%JavaPath%\java.exe" -version 2^>^&1') do if /I "%%A" == "version" set "JavaVersion=%%~B" & goto HaveJavaVersion

:JavaNotInstalled
echo Java is not installed.
goto EndBatch

:HaveJavaVersion
echo Current version of Java is: %JavaVersion%

:EndBatch
endlocal

I would use just the solution querying Path from HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\javaws.exe or even better the default value of this registry key on existing at all and running java.exe if existing to get its version. But it was not described which Java version is really wanted and for which versions of Windows should be written this batch file. I have added extra code to work also on Windows XP and Windows Server 2003 (not tested). REG on Windows XP and Windows Server 2003 outputs the information different to REG on Windows Vista/Server 2008 and later Windows versions as explained by comments in code of answer on Read words separated by space & string value also contains space in a batch script.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • reg /?
  • reg query /?
  • rem /?
  • set /?
  • setlocal /?


来源:https://stackoverflow.com/questions/51536574/unabled-to-determine-java-version-when-running-bat-command-from-within-qt-insta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!