How to Access Windows Registry From Java

Written by jackson lewis
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Access Windows Registry From Java
The Java programming language can access the Windows registry. (Java hot and black image by Pix by Marti from

A common problem for advanced Java developers is accessing the Windows registry from within a Java application or applet. There is not an embedded library within Java that provides the capability to access the Windows registry, however, the JNI Registry library is available to use for free in your Java programming. The JNI Registry is a Sun-approved Java library that provides Java classes to use in your software development.

Skill level:
Moderately Challenging


  1. 1

    Download the JNI Registry library (see Resources).

  2. 2

    Extract the registry library files by right-clicking the downloaded zip file and choosing the "Extract To" menu option to select the location for the file libraries.

  3. 3

    Open your Java integrated development environment (IDE) by double-clicking the program icon located on your computer's desktop. Choose the specific menu option to start a new Java class in the IDE by selecting the "File" and "New Java Class" menu options.

  4. 4

    Import the JNI registry class files into the new Java class. To import the classes, paste the following code at the top of the blank class file:

    import; import; import; import;

  5. 5

    Create new Registry and RegistryKey class instances. After creating the instances, open the registry subkey that you want to modify in your Java program. The following code can be used to accomplish these steps:

    Registry my Registry = new Registry(); Registry myRegistryKey= Registry.HKEY_CURRENT_USER; Registry my Key = registry.openSubkey(myRegistryKey, "Software\Microsoft\CurrentVersion\Explorer\Advanced",RegistryKey.ACCESS_ALL);

  6. 6

    Obtain access to the key value in the registry setting that you desire to modify and then change the value. When you access the Windows registry in Java, it must be done in a "try loop" in case a Registry Exception is thrown by Java in order to prevent the Java program from failing. After the try loop has been run in the Java program, the registry operations will be completed. The following code can be used to do this step:

    try { RegsitryValue myRegistryValue= myKey.getValue("ok"); myKey.setValue("Test Value", myRegistryValue); myKey.deleteValue("ok"); } catch(RegistryException re Ex) {}

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.