If you want to access the windows registry with Java you can use the Java
Runtime class
Runtime.getRuntime().exec("REG ADD HKCU\\SOFTWARE\\TEST")
Here is a little sample application i've wrote to show some possibilities of the windows commandline registry tool in combination with the Java Runtime class.
This code uses the m$ reg.exe tool.
If you want to create, modify or delete special registry keys like HKEY_LOCAL_MACHINE or HKEY_CURRENT_CONFIG you may need elevated privileges on your system.
import java.io.IOException;
import java.util.Scanner;
public class WinReg {
public static final int REG_SUCCESS = 0;
public static final int REG_FAILURE = 1;
private enum WRKey {
HKLM, HKCU , HKCR , HKU , HKCC
}
private enum WRType {
REG_SZ, REG_MULTI_SZ, REG_EXPAND_SZ,
REG_DWORD, REG_QWORD, REG_BINARY, REG_NONE
}
public String createRegString(WRKey hkey, String key, String valueName, byte[] data, WRType type, boolean force) {
String keyString = " "+hkey+"\\" + key;
String valueString = valueName!=null ? " /v "+ valueName : "" ;
String dataString = data != null ? (" " + ( data.length>0 ? " /d " + new String(data) : "")):"";
String typeString = type != null ? " /t " + type : "";
return keyString + valueString + dataString + typeString + (force ? " /f" : "");
}
public boolean addKey(WRKey hkey, String key) throws IOException, InterruptedException{
Process proc = Runtime.getRuntime().exec("REG ADD "+hkey+"\\" + key + " /f");
proc.waitFor();
return proc.exitValue() == REG_SUCCESS;
}
public boolean addValue(WRKey hkey, String key, String valueName, byte[] data, WRType type) throws IOException, InterruptedException {
String regString = createRegString(hkey,key,valueName,data,type,true);
Process proc = Runtime.getRuntime().exec("REG ADD " + regString);
proc.waitFor();
return proc.exitValue() == REG_SUCCESS;
}
public boolean showValue(WRKey hkey, String key, String valueName) throws IOException, InterruptedException{
String regString = createRegString(hkey,key,valueName,null,null,true);
Process proc = Runtime.getRuntime().exec("REG QUERY " + regString);
proc.waitFor();
if(proc.exitValue()==REG_SUCCESS) {
Scanner sc = new Scanner(proc.getInputStream());
String str = "";
do {
str = sc.nextLine();
System.out.println(str);
}while(str != null);
}
else {
System.err.println("Query failure..");
}
return proc.exitValue()==REG_SUCCESS;
}
public boolean showAllValues(WRKey hkey, String key, String valueName, boolean withChildren) throws IOException, InterruptedException {
String regString = createRegString(hkey,key,null,null,null,false);
Process proc = Runtime.getRuntime().exec("REG QUERY " + regString + "\\" + valueName + " " + (withChildren? " /s" :" "));
proc.waitFor();
if(proc.exitValue()==REG_SUCCESS) {
Scanner sc = new Scanner(proc.getInputStream());
String str = "";
do {
str = sc.nextLine();
System.out.println(str);
}while(sc.hasNext() && str != null);
if(sc!=null) {
sc.close();
}
}
else {
System.err.println("Query failure..\n" + regString);
}
return proc.exitValue()==REG_SUCCESS;
}
public static void main(String ... args) {
WinReg reg = new WinReg();
try {
if( reg.addKey(WRKey.HKCU, "SOFTWARE\\AAAA") ) {
System.out.println("Key created");
}
else {
System.err.println("Error: could not create the key");
}
if( reg.addValue(WRKey.HKCU, "SOFTWARE\\AAAA", "sampleValue", "Tesalue".getBytes(), WRType.REG_SZ) ) {
System.out.println("value created");
}
else {
System.err.println("Error: could not create the value");
}
if(! reg.showAllValues(WRKey.HKCU, "SOFTWARE\\AAAA", "", true) ) {
System.err.println("Error: could not show the values");
}
}
catch( InterruptedException | IOException e) {
System.err.println("An error occurred. " + e.getMessage() );
}
}
}