如何将Java Runtime.exec()与Windows注册表实用程序一起使用,以读取/更新/删除HKEY_LOCAL_MACHINE\…\CurrentVersion\Run中的条目?
我想使用Runtime.exec()更新如何将Java Runtime.exec()与Windows注册表实用程序一起使用,以读取/更新/删除HKEY_LOCAL_MACHINE\…\CurrentVersion\Run中的条目?,java,java-native-interface,32bit-64bit,registry,runtime.exec,Java,Java Native Interface,32bit 64bit,Registry,Runtime.exec,我想使用Runtime.exec()更新HKEY\U LOCAL\U MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 使用WindowsREG命令实用程序 需要能够添加/删除/读取“Run”键中的条目,以允许我的Swing应用程序在启动时运行,并检查它是否配置为在启动时运行,以便我可以在GUI中将该选项标记为选中或未选中。我用JNI做了这个,但是这个库只有32位,所以不能用64位。我认为这将是一个更好的方法。甚至不需要以这种方式包含库,
HKEY\U LOCAL\U MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
使用WindowsREG
命令实用程序
需要能够添加/删除/读取“Run”键中的条目,以允许我的Swing应用程序在启动时运行,并检查它是否配置为在启动时运行,以便我可以在GUI中将该选项标记为选中或未选中。我用JNI做了这个,但是这个库只有32位,所以不能用64位。我认为这将是一个更好的方法。甚至不需要以这种方式包含库,我认为REG
不会消失或改变
以前有没有人这样做过,或者知道怎么做
谢谢 此方法可能无法按预期工作。请参见我在这里的示例中添加了两个新方法(addValue/deleteValue):
/**
*@作者Oleg Ryaboy,基于Miguel Enriquez的作品
*/
公共类WindowsRequesty
{
/**
*
*@param位置
*注册表中的路径
*@param-key
*注册表项
*@返回注册表值,如果未找到,则返回null
*/
公共静态最终字符串读取注册表(字符串位置,字符串键)
{
尝试
{
//运行reg查询,然后使用StreamReader读取输出(内部类)
Process Process=Runtime.getRuntime().exec(“reg query\”“+location+”\“/v\”“+key+”);
StreamReader=新的StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
字符串输出=reader.getResult();
//输出具有以下格式:
//\n\n\n\t\t
如果(!output.contains(“\t”))
{
返回null;
}
//解析出值
String[]parsed=output.split(“\t”);
如果(解析长度>0)
{
字符串结果=已解析[parsed.length-1].trim();
result=result.substring(1,result.length()-1);
返回结果;
}
}
捕获(例外e)
{
}
返回null;
}
静态类StreamReader扩展线程
{
私有输入流是;
私有StringWriter sw=新StringWriter();;
公共流阅读器(InputStream is)
{
this.is=is;
}
公开募捐
{
尝试
{
INTC;
而((c=is.read())!=-1)
sw.write(c);
}
捕获(IOE异常)
{
}
最后
{
尝试
{
is.close();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
}
公共字符串getResult()
{
返回sw.toString();
}
}
公共静态布尔deleteValue(字符串键、字符串值名称)
{
尝试
{
//运行reg查询,然后使用StreamReader读取输出(内部类)
Process Process=Runtime.getRuntime().exec(“reg delete\”“+key+“\”/v\”“+valueName+“\”/f”);
StreamReader=新的StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
字符串输出=reader.getResult();
//输出具有以下格式:
//\n\n\n\t\t
返回output.contains(“操作成功完成”);
}
捕获(例外e)
{
}
返回false;
}
公共静态布尔addValue(字符串键、字符串valName、字符串val)
{
尝试
{
//运行reg查询,然后使用StreamReader读取输出(内部类)
Process Process=Runtime.getRuntime().exec(
“reg add\”“+key+”\“/v\”“+valName+”\“\”/d\“\\”“+val+”\\\“\\”/f”);
StreamReader=新的StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
字符串输出=reader.getResult();
//输出具有以下格式:
//\n\n\n\t\t
返回output.contains(“操作成功完成”);
}
捕获(例外e)
{
}
返回false;
}
}
重复。请看,这适用于XP,但不适用于Vista(均为32位)。我猜这是因为用户访问控制。当我刚从命令提示符下运行这些命令时,我得到了“ERROR:Access Denied”,但它们在XP上工作。不知道如何解决这个问题。
/**
* @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");
if(parsed.length > 0)
{
String result = parsed[parsed.length - 1].trim();
result = result.substring(1, result.length() - 1);
return result;
}
}
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)
{
}
finally
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public String getResult()
{
return sw.toString();
}
}
public static boolean deleteValue(String key, String valueName)
{
try
{
// Run reg query, then read output with StreamReader (internal class)
Process process = Runtime.getRuntime().exec("reg delete \"" + key + "\" /v \"" + valueName + "\" /f");
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>
return output.contains("The operation completed successfully");
}
catch (Exception e)
{
}
return false;
}
public static boolean addValue(String key, String valName, String val)
{
try
{
// Run reg query, then read output with StreamReader (internal class)
Process process = Runtime.getRuntime().exec(
"reg add \"" + key + "\" /v \"" + valName + "\" /d \"\\\"" + val + "\\\"\" /f");
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>
return output.contains("The operation completed successfully");
}
catch (Exception e)
{
}
return false;
}
}