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.
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.
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.
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.
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_USERunless 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.
Related reading
- reason no instances of type variables T exist so that void conforms to using mockito
- Reasons for using a Bag in Java
- Receiving Kafka Key in spring boot kafka listener
- Recommended way to get hostname in Java
- Recover an Asynch ThreadPoolTaskexecutor after server crashed/shut down
- Recursively list files in Java
- Redeploy alternatives to JRebel
- Redeploy spring-boot application in docker container?
Course lessons on this
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.