简体   繁体   中英

How to write windows registry using Java?

I am not very well versed in using the Preference Class. However from reading on here and other websites I feel that what I have should be enough to write into a node of my choosing.

Preferences userPref = Preferences.userRoot();
    userPref = userPref.node("HKEY_CURRENT_USER\\Software\\Broadcom");
    userPref.put("A", "B");

When trying to write into the node "HKEY_CURRENT_USER\\Software\\Broadcom" will only writer into the JavaSoft node. Is there anyway to change the subkey to allow me to write into any node I'd like?

PS I have tried writing the access location multiple times in different ways. With forward slashes instead of back slashes, with slashes in front of "HKEY", etc.

Anyone have any ideas?

Download jna libraries from here . Add jna-4.1.0.jar and jna-platform-4.1.0.jar files to your application classpath. And use Advapi32Util utility class to read, write or delete the registery keys. I test some of the solutions but most of them dont work. but jna version works pretty well and has lots of features.

Here is my test code to update Chromium lastrun value.

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

public class Main {

    public static void main(String[] args) throws Exception {
        Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER, "Software\\Chromium", "lastrun", "13031598735788802");
    }
}

The Preferences API does not necessarily provide access to the registry at all. Some implementations may use the registry, but you can't count on that. And it's likely that if an implementation does use the registry, the root of the Preferences hierarchy will be under "HKEY_CURRENT_USER\\Software\\Java" or something like that, and you won't be able to access anything outside that tree.

In general, if your question takes the form "How do I use [platform specific feature] in Java?", the answer is either "use JNI" or "don't use Java."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM