Java
Windows Registry
Programming
Code Writing
Software Development

Read and write the Windows registry using Java

Codemia Knowledge Hub ·

Java's standard library has no general-purpose Windows Registry API, because Java is cross-platform. For your application's own settings, java.util.prefs.Preferences is usually enough. For arbitrary registry keys, use JNA or an external command such as reg.exe.

Built-in option: Preferences

The Preferences API is the closest thing to built-in registry support. On Windows, it uses the registry as the backing store for user and system preference nodes.

java
1import java.util.prefs.Preferences;
2
3public class RegistryPrefsDemo {
4    public static void main(String[] args) {
5        Preferences prefs = Preferences.userRoot().node("com/example/myapp");
6
7        prefs.put("theme", "dark");
8        String theme = prefs.get("theme", "light");
9
10        System.out.println(theme);
11    }
12}

Use Preferences when:

  • your app only needs to store its own configuration
  • you want a cross-platform API
  • you do not need to access arbitrary registry hives or value types

It is not a registry browser. Think of it as Java-managed preferences that happen to live in the registry on Windows. When it or a plain config file covers your needs, it is simpler and safer than direct registry access.

Reading arbitrary keys with JNA

For direct access to hives such as HKEY_LOCAL_MACHINE, a common approach is JNA.

java
1import com.sun.jna.platform.win32.Advapi32Util;
2import com.sun.jna.platform.win32.WinReg;
3
4public class JnaRegistryRead {
5    public static void main(String[] args) {
6        String productName = Advapi32Util.registryGetStringValue(
7            WinReg.HKEY_LOCAL_MACHINE,
8            "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
9            "ProductName"
10        );
11
12        System.out.println(productName);
13    }
14}

This gives much more direct access than Preferences, but ties the code to Windows.

Writing with JNA

Writing works the same way, but permissions matter much more.

java
1import com.sun.jna.platform.win32.Advapi32Util;
2import com.sun.jna.platform.win32.WinReg;
3
4public class JnaRegistryWrite {
5    public static void main(String[] args) {
6        Advapi32Util.registryCreateKey(
7            WinReg.HKEY_CURRENT_USER,
8            "SOFTWARE\\ExampleApp"
9        );
10
11        Advapi32Util.registrySetStringValue(
12            WinReg.HKEY_CURRENT_USER,
13            "SOFTWARE\\ExampleApp",
14            "Mode",
15            "Enabled"
16        );
17    }
18}

Writes under HKEY_LOCAL_MACHINE often require elevation, so handle permission failures. HKEY_CURRENT_USER is usually simpler for application settings.

Calling reg.exe as a fallback

You can also run the native Windows reg command from Java.

java
1import java.io.IOException;
2
3public class RegistryViaRegExe {
4    public static void main(String[] args) throws IOException {
5        Process process = new ProcessBuilder(
6            "reg",
7            "query",
8            "HKCU\\SOFTWARE\\ExampleApp",
9            "/v",
10            "Mode"
11        ).start();
12    }
13}

It is less elegant than JNA, but for a small, Windows-only need it can be easier to deploy than a native-access library.

Writing safely

Registry writes are privileged configuration changes, not ordinary file writes. Mistakes can break application settings or system behavior. So:

  • write only under keys your application owns
  • prefer HKEY_CURRENT_USER unless machine-wide configuration is really required
  • avoid editing unrelated system keys from application code

Any registry access through JNA or reg.exe also makes the code Windows-specific.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms