Is it possible to read/write to windows registry using java?
Yes, using the java.util.Preferences API, since the Windows implementation of it uses the Registry as a backend.
In the end it depends on what you're wanting to do: storing preferences for your app is what the Preferences does just great. If you're wanting to actually change registry keys not having to do with your app, you'll need some JNI app, as described by Mark (shameless steal here):
From a quick google: Check the WinPack for JNIWrapper. It has full Windows Registry access support including Reading and Writing.
The WinPack Demo has Registry Viewer implemented as an example.
Check at http://www.jniwrapper.com/winpack_features.jsp#registry
And...
There is also try JNIRegistry @ http://www.trustice.com/java/jnireg/
There is also the option of invoking an external app, which is responsible for reading / writing the registry.
From a quick google:
Check the WinPack for JNIWrapper. It has full Windows Registry access support including Reading and Writing.
The WinPack Demo has Registry Viewer implemented as an example.
Check at http://www.jniwrapper.com/winpack_features.jsp#registry
And...
There is also try JNIRegistry @ http://www.trustice.com/java/jnireg/
There is also the option of invoking an external app, which is responsible for reading / writing the registry.
The Preferences API approach does not give you access to all the branches of the registry. In fact, it only gives you access to where the Preferences API stores its, well, preferences. It's not a generic registry handling API, like .NET's
To read/write every key I guess JNI or an external tool would be the approach to take, as Mark shows.
There are few JNDI service providers to work with windows registry.
One could observe http://java.sun.com/products/jndi/serviceproviders.html
I've done this before using jRegistryKey. It is an LGPL Java/JNI library that can do what you need. Here's an example of how I used it to enabled Registry editing through regedit and also the "Show Folder Options" option for myself in Windows via the registry.
import java.io.File;
import ca.beq.util.win32.registry.RegistryKey;
import ca.beq.util.win32.registry.RegistryValue;
import ca.beq.util.win32.registry.RootKey;
import ca.beq.util.win32.registry.ValueType;
public class FixStuff {
private static final String REGEDIT_KEY = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
private static final String REGEDIT_VALUE = "DisableRegistryTools";
private static final String REGISTRY_LIBRARY_PATH = "\\lib\\jRegistryKey.dll";
private static final String FOLDER_OPTIONS_KEY = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer";
private static final String FOLDER_OPTIONS_VALUE = "NoFolderOptions";
public static void main(String[] args) {
//Load JNI library
RegistryKey.initialize( new File(".").getAbsolutePath()+REGISTRY_LIBRARY_PATH );
enableRegistryEditing(true);
enableShowFolderOptions(true);
}
private static void enableShowFolderOptions(boolean enable) {
RegistryKey key = new RegistryKey(RootKey.HKEY_CURRENT_USER,FOLDER_OPTIONS_KEY);
RegistryKey key2 = new RegistryKey(RootKey.HKEY_LOCAL_MACHINE,FOLDER_OPTIONS_KEY);
RegistryValue value = new RegistryValue();
value.setName(FOLDER_OPTIONS_VALUE);
value.setType(ValueType.REG_DWORD_LITTLE_ENDIAN);
value.setData(enable?0:1);
if(key.hasValue(FOLDER_OPTIONS_VALUE)) {
key.setValue(value);
}
if(key2.hasValue(FOLDER_OPTIONS_VALUE)) {
key2.setValue(value);
}
}
private static void enableRegistryEditing(boolean enable) {
RegistryKey key = new RegistryKey(RootKey.HKEY_CURRENT_USER,REGEDIT_KEY);
RegistryValue value = new RegistryValue();
value.setName(REGEDIT_VALUE);
value.setType(ValueType.REG_DWORD_LITTLE_ENDIAN);
value.setData(enable?0:1);
if(key.hasValue(REGEDIT_VALUE)) {
key.setValue(value);
}
}
}
The WinPack Demo has Registry Viewer implemented as an example.
Check at http://www.jniwrapper.com/winpack_features.jsp#registry
BTW, WinPack has been moved to the following address:
http://www.teamdev.com/jniwrapper/winpack/
how to write windows services in java
WinPack also supports for windows services management: http://www.teamdev.com/jniwrapper/winpack/#services
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)
You don't actually need a 3rd party package. Windows has a reg utility for all registry operations. To get the command format, go to the DOS propmt and type:
reg /?
You can invoke reg through the Runtime class:
Runtime.getRuntime().exec("reg <your parameters here>");
Editing keys and adding new ones is straightforward using the command above. To read the registry, you need to get reg's output, and it's a little tricky. Here's the code:
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
/**
* @author Oleg Ryaboy, based on work by Miguel Enriquez
*/
public class WindowsReqistry {
/**
*
* @param location path in the registry
* @param key registry key
* @return registry value or null if not found
*/
public static final String readRegistry(String location, String key){
try {
// Run reg query, then read output with StreamReader (internal class)
Process process = Runtime.getRuntime().exec("reg query " +
'"'+ location + "\" /v " + key);
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
String output = reader.getResult();
// Output has the following format:
// \n<Version information>\n\n<key>\t<registry type>\t<value>
if( ! output.contains("\t")){
return null;
}
// Parse out the value
String[] parsed = output.split("\t");
return parsed[parsed.length-1];
}
catch (Exception e) {
return null;
}
}
static class StreamReader extends Thread {
private InputStream is;
private StringWriter sw= new StringWriter();;
public StreamReader(InputStream is) {
this.is = is;
}
public void run() {
try {
int c;
while ((c = is.read()) != -1)
sw.write(c);
}
catch (IOException e) {
}
}
public String getResult() {
return sw.toString();
}
}
public static void main(String[] args) {
// Sample usage
String value = WindowsReqistry.readRegistry("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\"
+ "Explorer\\Shell Folders", "Personal");
System.out.println(value);
}
}