Read and write to the MS Windows Registry from Java


Sometimes you need to read or write to the MS Windows Registry (be careful!).

This can normally not be done from Java, but by using JNA you can access the MS Windows API.


In JNA a lot of the MS Windows API has already been mapped so it is ready for use.

This is a short simple example on how the get the installed Flash version:
	root=HKEY_LOCAL_MACHINE;
	keyStr="SOFTWARE\\Macromedia\\FlashPlayer";
	valueStr="CurrentVersion";
		
	if (Advapi32Util.registryKeyExists(root,keyStr)) {
		System.out.println(Advapi32Util.registryGetStringValue(root,keyStr,valueStr));
	}else {
		System.out.println("Could not find " + keyStr +"\\"+valueStr);
	}


This will print: 21,0,0,213

Take a look here for the other methods for handling the Windows Registry:

https://java-native-access.github.io/jna/4.2.1/com/sun/jna/platform/win32/Advapi32.html


Posted on 04/15/2016 04:16:31 PM CEDT