Search code examples
javaregistryinstall4j

How to use registry actions


Does anybody know how to use the read from or write to Windows Registry actions? All I get are failures when trying.

An example would be trying to read from HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit the CurrentVersion key, which I know is there and set, fails to read. I set registry root to HKEY_LOCAL_MACHINE and key name to SOFTWARE\JavaSoft\Java Development Kit and value name to CurrentVersion but it still fails. Any help would be really appreciated.

Thanks.


Solution

  • Most likely you're running a 32-bit installer, but you're looking for the value in the 64-bit registry.

    32-bit executables have their own software settings under HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node. The registry actions always read from the registry view that corresponds to the bitness of the installer. If you have installed a 64-bit JDK and no 32-bit JDK, the registry value you're interested in will be empty in the 32-bit view of the registry.

    To check if this is really the problem in your case, try to select "Generate 64-bit executables" in the "32-bit or 64-bit" step of the media wizard, it should then work as expected.

    With the API, you can set the registry view explicitly:

    Object value = WinRegistry.getValue(
        RegistryRoot.HKEY_LOCAL_MACHINE, 
        "SOFTWARE\\JavaSoft\\Java Development Kit",
        "CurrentVersion", 
        RegistryView.BIT64
    );
    if (value != null) {
        context.setVariable("javaCurrentVersion", value.toString()); 
    }
    

    By using RegistryView.BIT64 and RegistryView.BIT32 you can access the 64-bit view from a 32-bit installer and the 32-bit view from a 64-bit installer.