I need to create a new variable in the Registry key and put a value into it, using com.ice.jni.registry
. Examples which I have found on the internet don't explain how to do it. I know how to get the existing value, and set a new value in the existing key.\\
I Find some code wich shoud edit the value if it is already created. Obvious i am getting "NullPointerException"
try {
Registry registry = new Registry();
registry.debugLevel=true;
RegistryKey regkey = Registry.HKEY_CURRENT_USER;
RegistryKey key =registry.openSubkey(regkey,"Software\\Microsoft\\4Game\\AceOnline",RegistryKey.ACCESS_ALL);
RegStringValue stringValue = new RegStringValue(key,"javaci",RegistryValue.REG_SZ);
stringValue.setData("Hello");
key.setValue(stringValue); //NullPointer Exception here
}
catch(RegistryException ex) {
ex.printStackTrace();
}
It can be a real pain to get anything done with JNI because: 1. The DLL must be on the path. 2. You can't copy a DLL to system32 because of the added security in Vista and Windows 7 3. The DLL must be available in both 32 and 64 bit versions in case the JVM being used is 32 or 64 bit.
You can avoid a lot of problems by just calling the reg command line tool built into Windows.
In a command prompt type reg add /?
You can call a command like this using Runtime.getRuntime().exec().
You're in luck. I have some code that uses this from Java. It's not very pretty though so good luck; I make no guarantees that it works and won't destroy your registry.
public class WinUtil {
//returns mappings as (extension, mimetype) (without periods)
public static Map<String, String> getMimeMappings() throws Exception {
Map<String, String> extensions = new HashMap<String, String>();
String result = WinUtil.doRegQuery("HKEY_CLASSES_ROOT\\Mime\\Database\\Content Type", "/s");
if (result == null) {
return null;
}
String path = null;
String[] lines = result.split("\n");
for (String line : lines) {
line = line.replaceAll("\r", "");
if (line.startsWith(" ") || line.startsWith(" ")) {
line = line.trim();
if (line.startsWith("Extension")) {
String[] entries = line.split(" ");
if (entries.length >= 3) {
String ext = entries[2].substring(1); //skip .
// we only split here, since Extension may not have been found, in which case it would have been a waste
String[] pathSplit = path.split("\\\\");
String key = pathSplit[pathSplit.length - 1];
extensions.put(ext.toLowerCase(), key.toLowerCase());
}
}
} else {
line = line.trim();
path = line;
}
}
return extensions;
}
//return key locations and keys
public static String[] getRegSubKeys(String regLocation) {
try {
String result = doRegQuery(regLocation, "");
if (result == null) {
return null;
} else if (result.isEmpty()) {
return new String[0]; // empty string array
}
String[] strArray = result.split("\n");
//remove form-feed characters, if any
for (int i = 0; i < strArray.length; i++) {
strArray[i] = strArray[i].replaceAll("\r", "");
}
return strArray;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//note when using this to use the abbreviated forms: i.e.
// HKCU, HKLM, etc.
public static String setRegKey(String location, String regKey, String regValue) throws Exception {
String regCommand = "add \"" + location + "\" /v \"" + regKey + "\" /f /d \"" + regValue + "\"";
return doReg(regCommand);
}
public static String getRegKey(String regKey, String regKeyType, String regLocation) throws Exception {
String regCommand = "/v \"" + regKey + "\"";
String result = doRegQuery(regLocation, regCommand);
int p = result.indexOf(regKeyType);
if (p == -1) {
throw new Exception("Could not find type of key in REG utility output");
}
return result.substring(p + regKeyType.length()).trim();
}
//may return null if exec was unsuccessful
public static String doRegQuery(String regLocation, String regCommand) throws Exception {
final String REGQUERY_UTIL = "Reg Query" + " ";
final String regUtilCmd = REGQUERY_UTIL + "\"" + regLocation + "\" " + regCommand;
return runProcess(regUtilCmd);
}
public static String doReg(String regCommand) throws Exception {
final String REG_UTIL = "reg";
final String regUtilCmd = REG_UTIL + " " + regCommand;
return runProcess(regUtilCmd);
}
public static String runProcess(final String regUtilCmd) throws Exception {
StringWriter sw = new StringWriter();
Process process = Runtime.getRuntime().exec(regUtilCmd);
InputStream is = process.getInputStream();
int c = 0;
while ((c = is.read()) != -1) {
sw.write(c);
}
String result = sw.toString();
try {
process.waitFor();
} catch (Throwable ex) {
System.out.println(ex.getMessage());
}
if (process.exitValue() == -1) {
throw new Exception ("REG QUERY command returned with exit code -1");
}
return result;
}
}
如果可以掌握要创建的Key的父级,则可以尝试使用RegistryKey.createSubKey(java.lang.String subkey,java.lang.String className)
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.