简体   繁体   中英

To get only 64 bit JRE value from Windows Registry

As i am from testing background and have a very limited knowledge on Java kindly excuse me if the question i have asked is not good or repetitive.

I am trying to build an application using Java Swing. As per the requirement I have to display 32 bit Jre or 64 bit Jre installed on laptop based on the selection made. Frame which i developed for this

Unfortunately when I tested by installing a 32 bit JRE on Windows 64 bit machine and tried to run my application, when I choose the radio button "64 bit JRE" , in the drop down list its displaying me the 32 bit JRE which is installed on my system. (Kindly note i do not have any 64 bit JRE installed on my system). Ideally it should not display anything but the default value set in the JComboBox. :(

I understood the JRE was picked due to the Windows property. I tried with the API Advapi32.INSTANCE using the method RegGetValue but i am unable to get the value from Windows Registry.

32 bit JRE are under the path : HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node \\JavaSoft\\Java Runtime Environment and 64 bit JRE are under the path : HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment

Please find the below code which i tried to retrieve 64 bit JRE :

String[] val64 = RegistryCheck.getRegistryDataForJRE(HKEY_LOCAL_MACHINE, "SOFTWARE\\JavaSoft\\Java Runtime Environment"); 

public static String[] getRegistryDataForJRE(WinReg.HKEY root, String key) {

    if (Advapi32Util.registryKeyExists(root, key)) {
        return Advapi32Util.registryGetKeys(root, key);
    } else {
        return null;
    }
} 

Also below is the code which i tried with Advapi32.INSTANCE (just ttried to do a sysout to know whether the value returned is 0 or not. as from the Microsoft pages i see if its a non zero value returned its an error

public static void main(String[] args) {

    byte[] b = new byte[50];
    IntByReference pcbData = new IntByReference(50);

    System.out.println(Advapi32.INSTANCE.RegGetValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\JavaSoft\\Java Runtime Environment",
        "" , 0x0200, pcbData, b, pcbData));

}

Also, I read the flag KEY_WOW64_32KEY is the responsible to pick up from the WOW6432Node , but i am not able to check the flag value or to set it to false in java.

Can anyone help me with this issue ?

I would be really grateful for the help.

Thank you in advance for the support.

try to use the method RegOpenKeyEx() of Advapi32 ie syntax

RegOpenKeyEx(WinReg.HKEY hKey,
                 String lpSubKey,
                 int ulOptions,
                 int samDesired,
                 WinReg.HKEYByReference phkResult)

refer to the link RegOpenKeyEx()

You can pass the parameters accordingly and then check if the return value is "0" then the path exists in 64 bit else it doesn't exists.

Note : when you pass "samDesired" into the RegOpenKeyEx() pass the combination of KEY_QUERY_VALUE and KEY_WOW64_64KEY

For example :

WinReg.HKEYByReference hKey = new HKEYByReference();
Advapi32.INSTANCE.RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\JavaSoft\\Java Runtime Environment",0,  0x0001 | 0x0100,  hKey);

Let me know if it worked for you

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