Search code examples
javaregistry

Registry checking if path/key exists


Hei guys, quick stupid question...

I got this code:

String arch = System.getenv("PROCESSOR_ARCHITECTURE");
String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432");
String realArch = arch.endsWith("64") || wow64Arch != null && wow64Arch.endsWith("64") ? "64" : "32";
String setWin = ((realArch.contains("64")) ? "SOFTWARE\\Wow6432Node\\path" : "SOFTWARE\\path");
String check = Advapi32Util.registryGetStringValue (HKEY_LOCAL_MACHINE, setWin, "InstallDir");

Which strings a path to a installed application, but if the registry doesn't exists my program stops. How i can check if the registry exists and if doesn't bypass it?

and also to add system.err.println("Application not installed."); if the key doesn't exists.

PS: The program is a GUI so i want to show it even if the key is missing.

Thank you!


Solution

  • Thank you, Sam. I did found something, not sure if it will fail at one point ...

    public void checkInstalled(){
        try {
            String regValue = null;
                regValue = WinRegistry.valueForKey(WinRegistry.HKEY_LOCAL_MACHINE, setWin, "InstallDir");
            if(regValue == null){
                System.err.println("Application not installed!");
            } else {
                "do the other thing"
                }
            } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException | IOException ex) {
            System.err.println(ex);
            }
        }