Java - Edit Windows Registry

Asked

Viewed 988 times

3

How can I change a Windows registry (regedit)?
And if the key doesn’t exist How can I create?

1 answer

2

A java program should not need to write in the windows registry, since the main reason to use java should be platform independence, in my opinion. However, theory is theory and practice is practice. Therefore, I will not here try to dissuade you from using windows registry in a java application, as this would not answer the question and would lead to a discussion perhaps too large in the comments.

The most common way that the community uses to interact with code native to the Windows Operating System is to jna library.

Specifically, after adding the jar to your project, you should use the package com.sun.jna.platform.win32.Advapi32Util:

import com.sun.jna.platform.win32.Advapi32Util

To check if a key exists, the method is as follows::

Advapi32Util.registryKeyExists(WinReg.HKEY root, java.lang.String key);

Signing:

public static boolean registryKeyExists(WinReg.HKEY root,
                                        java.lang.String key)
  1. The First parameter receives possible values:

    • WinReg.HKEY_CURRENT_USER
    • WinReg.HKEY_LOCAL_MACHINE
    • etc.
  2. The second parameter is the key name.

To read a value, use:

String:

public static java.lang.String registryGetStringValue(WinReg.HKEY root,
                                                          java.lang.String key,
                                                          java.lang.String value)

All-purpose:

public static int registryGetIntValue(WinReg.HKEY root,
                                          java.lang.String key,
                                          java.lang.String value)

And so on, see some examples (English, but code is code): Example 1, Example 2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.