Read/write to Windows registry using Java

前端 未结 24 1565
日久生厌
日久生厌 2020-11-21 05:45

How is it possible to read/write to the Windows registry using Java?

相关标签:
24条回答
  • 2020-11-21 06:26

    As has been noted, the Preferences API uses the registry to store preferences, but cannot be used to access the whole registry.

    However, a pirate called David Croft has worked out that it's possible to use methods in Sun's implementation of the Preferences API for reading the Windows registry from Java without JNI. There are some dangers to that, but it is worth a look.

    0 讨论(0)
  • 2020-11-21 06:28

    The best way to write to the register probably is using the reg import native Windows command and giving it the file path to the .reg file which has been generated by exporting something from the registry.

    Reading is done with the reg query command. Also see the documentation: https://technet.microsoft.com/en-us/library/cc742028.aspx

    Therefore the following code should be self-explanatory:

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class WindowsRegistry
    {
        public static void importSilently(String regFilePath) throws IOException,
                InterruptedException
        {
            if (!new File(regFilePath).exists())
            {
                throw new FileNotFoundException();
            }
    
            Process importer = Runtime.getRuntime().exec("reg import " + regFilePath);
    
            importer.waitFor();
        }
    
        public static void overwriteValue(String keyPath, String keyName,
                String keyValue) throws IOException, InterruptedException
        {
            Process overwriter = Runtime.getRuntime().exec(
                    "reg add " + keyPath + " /t REG_SZ /v \"" + keyName + "\" /d "
                            + keyValue + " /f");
    
            overwriter.waitFor();
        }
    
        public static String getValue(String keyPath, String keyName)
                throws IOException, InterruptedException
        {
            Process keyReader = Runtime.getRuntime().exec(
                    "reg query \"" + keyPath + "\" /v \"" + keyName + "\"");
    
            BufferedReader outputReader;
            String readLine;
            StringBuffer outputBuffer = new StringBuffer();
    
            outputReader = new BufferedReader(new InputStreamReader(
                    keyReader.getInputStream()));
    
            while ((readLine = outputReader.readLine()) != null)
            {
                outputBuffer.append(readLine);
            }
    
            String[] outputComponents = outputBuffer.toString().split("    ");
    
            keyReader.waitFor();
    
            return outputComponents[outputComponents.length - 1];
        }
    }
    
    0 讨论(0)
  • 2020-11-21 06:28

    You could try WinRun4J. This is a windows java launcher and service host but it also provides a library for accessing the registry.

    (btw I work on this project so let me know if you have any questions)

    0 讨论(0)
  • 2020-11-21 06:29

    You can execute these "REG QUERY" command using java code.

    Try to execute this from command prompt and execute command from java code.

    HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows NT\CurrentVersion"

    To Search details like productname version etc.. use /v amd "name".

    HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "ProductName"

    0 讨论(0)
  • 2020-11-21 06:31

    The java.util.prefs package provides a way for applications to store and retrieve user and system preferences and data configuration. These preference data will be stored persistently in an implementation-dependent backing stored. For example in Windows operating system in will stored in Windows registry.

    To write and read these data we use the java.util.prefs.Preferences class. Below code shows how to read and write to the HKCU and HKLM in the registry.

    import java.util.prefs.Preferences;
    
    public class RegistryDemo {
        public static final String PREF_KEY = "org.username";
        public static void main(String[] args) {
            //
            // Write Preferences information to HKCU (HKEY_CURRENT_USER),
            // HKCU\Software\JavaSoft\Prefs\org.username
            //
            Preferences userPref = Preferences.userRoot();
            userPref.put(PREF_KEY, "xyz");
    
            //
            // Below we read back the value we've written in the code above.
            //
            System.out.println("Preferences = "
                    + userPref.get(PREF_KEY, PREF_KEY + " was not found."));
    
            //
            // Write Preferences information to HKLM (HKEY_LOCAL_MACHINE),
            // HKLM\Software\JavaSoft\Prefs\org.username
            //
            Preferences systemPref = Preferences.systemRoot();
            systemPref.put(PREF_KEY, "xyz");
    
            //
            // Read back the value we've written in the code above.
            //
            System.out.println("Preferences = "
                    + systemPref.get(PREF_KEY, PREF_KEY + " was not found."));
        }
    }
    
    0 讨论(0)
  • 2020-11-21 06:32

    There are few JNDI service providers to work with windows registry.

    One could observe http://java.sun.com/products/jndi/serviceproviders.html.

    0 讨论(0)
提交回复
热议问题