How to Access Windows Registry From Java

Posted by


The Windows registry is a complex, hierarchical database that includes a multitude of entries detailing information about just about anything done using Windows. Developers sometimes need to access the Windows registry from inside a Java applet or application in order to read from the registry, write to current keys, or add new keys, but this can be difficult since there is no outright function for this task in Java.

Instead, usually third party software is needed to access the registry. One such widely used free program is called the JNI Registry library. This Sun approved library can be used to access the Windows registry as well as for Java classes for software development.

The process can be somewhat complex and is best attempted by advanced users; commercially available software to access the Windows registry from Java is much simpler to use (often simply requiring a few clicks of the mouse) and usually quite cost effective. Almost any process to access the Windows registry without using commercial software will require codes, hacks, and at least one download, if not more. The following is one of the more straightforward methods, using the least amount of codes and downloads, to create access to the Windows registry from Java.

Visit a reputable site to download the registry library.

Right click the zip file you downloaded and select “Extract To,” then select the desired location for the files.

Double click the icon now on your desktop to open the Java IDE (integrated development environment). Once the program is open, select “File,” then click on “New Java Class” to start a new Java class.

You will now import the JNI registry class documents into your new class. To do this, you will need to paste a long code into the blank class file. This code should be sufficient:


Now you must add new RegistryKey and Registry class instances. Once that is complete, you can then open and edit the registry keys you were looking to access in your Java application or applet. Use this code for those purposes:

Registry myRegistry = new Registry();
Registry myRegistryKey= Registry.HKEY_CURRENT_USER;
Registry myKey = registry.openSubkey(myRegistryKey,

You can now get to the key value in the Windows registry setting to be accessed or modified and then alter its value. You must use a “try loop” to access the registry while in Java because sometimes Java will create a registry exception in an attempt to prevent the failure of the Java program. After running through the try loop while in Java, you will have complete registry options. The last code you need to for this final step is as follows:

try { RegsitryValue myRegistryValue= myKey.getValue(“ok”);
myKey.setValue(“Test Value”, myRegistryValue);
myKey.deleteValue(“ok”); }
catch(RegistryException reEx) {}

Another frequently used tool used to access the Windows registry can also be downloaded anywhere a copy of JWinAPI is available. This is a more complex tool than those described above, but is known to be effective.


Leave a Reply

Your email address will not be published. Required fields are marked *