Java
Windows Registry
Programming
Code Writing
Software Development

Read/write to Windows registry using Java

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Java does not expose a general-purpose Windows Registry API in the standard library because Java is designed to be cross-platform. You can still read and write registry-backed settings in a few different ways, but the approach depends on what you actually need. For lightweight application preferences, java.util.prefs.Preferences is usually enough. For arbitrary registry keys, you typically need JNA or an external command such as reg.exe.

The Easiest Built-In Option: Preferences

Java's Preferences API is the closest thing to built-in registry support. On Windows, it uses the registry as its 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}

This is good for app-owned settings, not for browsing arbitrary registry locations across the whole system.

What Preferences Is Good For

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 full Windows Registry exploration tool. Think of it as Java-managed preferences that happen to live in the registry on Windows.

For Arbitrary Keys, Use JNA

If you need direct access to registry hives such as HKEY_LOCAL_MACHINE, a common Java 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 you much more direct access than Preferences, but it also ties the code to Windows.

Writing with JNA

Writing works similarly, though permissions now 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. HKEY_CURRENT_USER is usually simpler for application settings.

reg.exe Is a Pragmatic Fallback

Another practical option is to call 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}

This is less elegant than JNA, but sometimes it is easier to deploy than a native-access library if the need is small and Windows-only anyway.

Be Careful with Permissions and Stability

Registry writes are not ordinary file writes. Mistakes can break application settings or system behavior. That is why the safest rule is:

  • 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

Also remember that registry access makes the code platform-specific.

Common Pitfalls

  • Assuming the Java standard library offers full arbitrary registry access when Preferences only covers a narrower use case.
  • Using JNA or reg.exe without accepting that the code is now Windows-specific.
  • Writing to protected hives such as HKEY_LOCAL_MACHINE without handling permission failures.
  • Treating registry edits like harmless application data instead of a privileged system configuration change.
  • Choosing direct registry access when a normal config file or Java Preferences node would be simpler and safer.

Summary

  • Java's built-in Preferences API is the easiest way to store app settings that map to the registry on Windows.
  • Use JNA when you need direct access to arbitrary registry keys or values.
  • 'reg.exe is a pragmatic fallback for simple Windows-only tasks.'
  • Prefer user-level keys over machine-level keys when possible.
  • Use registry access deliberately because it reduces portability and raises permission and stability concerns.

Related reading
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
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.