Java - Edit Windows Registry

3

How can I change a Windows registry (regedit)? What if the key does not exist how can I create it?

    
asked by anonymous 22.03.2015 / 17:10

1 answer

1

A java program should not need to write to the windows registry, as the main reason for using java should be platform independence, in my opinion. However, theory is theory and practice is practice. So I'm not going to try to dissuade you from using windows registry in a java application, as that would not answer the question and lead to a perhaps very large discussion in the comments.

The most common way the community uses to interact with native code for the windows operating system is 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);

Signature:

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

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

  • To read a value, use:

    For string:

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

    To Integer:

    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 < a>

        
    28.03.2015 / 23:09