jRegistryKey is an open-source Java Native Interface (JNI) library designed to read, create, and modify the Windows Registry from a Java application. It wraps low-level Windows Win32 API functions into standard, readable Java objects. Essential Prerequisites
Because jRegistryKey relies on native code, you must include two distinct files in your application layout:
jRegistryKey.jar: This holds the Java classes and must reside on your application’s CLASSPATH.
jRegistryKey.dll: This is the native Windows library. It must be placed in a directory covered by the java.library.path system property (such as C:\Windows\System32) or explicitly loaded.
Note: The original library compiled by the author natively targets 32-bit (x86) Java environments. If your JVM crashes with an UnsatisfiedLinkError, you may need to match your target architecture or switch to a 64-bit compiled fork. Core Classes to Know
RootKey: An enumeration mapping directly to the major Windows root hives, such as RootKey.HKEY_CURRENT_USER and RootKey.HKEY_LOCAL_MACHINE.
RegistryKey: Represents a folder-like registry key. You use this to navigate paths and manage subkeys.
RegistryValue: Represents a specific key-value property pair.
ValueType: Maps Windows data types like strings (ValueType.REG_SZ) and numbers (ValueType.REG_DWORD). How to Code Registry Operations
You can download the library binary distribution directly from jRegistryKey on SourceForge. To manipulate registry components, import ca.beq.util.win32.registry.* into your code: 1. Reading Subkeys and Values
To scan existing registry folders, initialize a RegistryKey object pointing to your target hive and subkey path.
import ca.beq.util.win32.registry.RegistryKey; import ca.beq.util.win32.registry.RootKey; import java.util.Iterator; public class ReadRegistry { public static void main(String[] args) { // Instantiate the key reference RegistryKey key = new RegistryKey(RootKey.HKEY_CURRENT_USER, “Software\Microsoft\Windows\CurrentVersion”); // Safely check for and iterate through nested subkeys if (key.hasSubkeys()) { Iterator<?> subkeys = key.subkeys(); while (subkeys.hasNext()) { System.out.println(“Found Subkey: ” + subkeys.next()); } } } } Use code with caution. 2. Creating Keys and Writing Values
When writing configuration data, use RegistryValue to pair your property name, the format type, and the destination value.
// Example: Creating a key and setting a value RegistryKey key = new RegistryKey(RootKey.HKEY_CURRENT_USER, “Software\MyCustomApp”); if (!key.exists()) key.create(); // Create if missing RegistryValue appSetting = new RegistryValue(“InstallPath”, ValueType.REG_SZ, “C:\Path”); key.setValue(appSetting); // Write value Use code with caution. 3. Deleting Values and Keys
How to Modify the Windows Registry. | Microsoft Community Hub
Leave a Reply