简体   繁体   中英

InnoSetup: Detect if Java is 32-bit or 64-bit

In InnoSetup I run this code:

J32 := ShellExec('', 'java', '-d32 -version', '', SW_HIDE, ewWaitUntilTerminated, ec);
J64 := ShellExec('', 'java', '-d64 -version', '', SW_HIDE, ewWaitUntilTerminated, ec);

Both J32 and J64 are True .

In command line:

> java -d32 -version
Error: This Java instance does not support a 32-bit JVM.
Please install the desired version.

> echo %errorlevel%
1

> java -d64 -version
java version "1.7.0"
Java(TM) SE Runtime Environment (build 1.7.0-b147)
Java HotSpot(TM) 64-Bit Server VM (build 21.0-b17, mixed mode)

> echo %errorlevel%
0

Why does ShellExec() ignore Params ?

I tried Exec() also:

// this way
J32 := Exec('java', '-d32 -version', '', SW_HIDE, ewWaitUntilTerminated, ec);
// and this way
J32 := Exec('>', 'java -d32 -version', '', SW_HIDE, ewWaitUntilTerminated, ec);

They all return True , and ec = 1 , despite the fact that I have a 64-bit java.

It seems that Exec and ShellExec return True because they succeed to run java , but they do not track the error code java returns.

The Inno Setup help states:

http://www.jrsoftware.org/ishelp/index.php?topic=setup_architecturesinstallin64bitmode

The System32 path returned by the {sys} constant maps to the 64-bit System directory by default when used in the [Dirs], [Files], [InstallDelete], [Run], [UninstallDelete], and [UninstallRun] sections. This is because Setup/Uninstall temporarily disables WOW64 file system redirection [external link] when files/directories are accessed by those sections. Elsewhere, System32 and {sys} map to the 32-bit System directory, as is normal in a 32-bit process.

So in 64-bit mode in the [Code] section, everything is 32-bit. It will execute 32-bit Java and c:\\Windows\\System32 points to the WOW64 folder, ie the 32-bit version of System32.

This answer shows how to check Java in the registry instead:

Need help on Inno Setup script - issue in check the jre install

Following that answer, the following code seems to work to check whether 64-bit Java 1.7+ is installed:

[Code]

function JavaIsMissing(): Boolean;
var 
javaVersionOutput: AnsiString;

begin

result := not RegQueryStringValue(HKLM64, 'Software\JavaSoft\Java Runtime Environment',
   'CurrentVersion', javaVersionOutput);
if not result then
   result := CompareStr(javaVersionOutput, '1.7') < 0;
end;

[Run]
Filename: "{tmp}\{#JavaInstaller}"; StatusMsg: "Java Runtime Enviroment not installed on your system. Installing..."; Check: JavaIsMissing

I wanted something I could use across multiple Inno Setup projects, so I wrote a DLL for detecting Java details (home directory, etc.):

https://github.com/Bill-Stewart/JavaInfo

Download from here: https://github.com/Bill-Stewart/JavaInfo/releases

The download includes a sample Inno Setup .iss script that demonstrates how to use the DLL functions (including how to check whether 32-bit or 64-bit).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM