改变与getRuntime()注册表项值。EXEC(CMD)不更改注册表值,甚至是操作成功结束 [英] Changing value of registry key with getRuntime().exec(cmd) does not change registry value even the operation ended successfully

查看:471
本文介绍了改变与getRuntime()注册表项值。EXEC(CMD)不更改注册表值,甚至是操作成功结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的Java应用程序更改注册表值。我用的命令:

  LoudnessEqualizationToggle.execCmdNoReturn(REG ADD \\\"HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\MMDevices\\\\Audio\\\\Render\\\\\"+guid+\"\\\\FxProperties\\\"\"
                                           +/ F / V \\{E0A941A0-88A2-4df5-8D6B-DD20BB06E8FB} 4 \\/吨REG_DWORD / d的\\+((activateLEOnKey)?1:0)+\\);


  

其中GUID是{d348b8e8-3118-4a9c-9b43-422647b555ca}


  
  

activateLEOnKey是布尔值


execCmdNoReturn 功能:

 公共静态最终无效execCmdNoReturn(字符串CMD){        尝试{
            扫描器S =新的扫描仪(调用Runtime.getRuntime()EXEC(CMD).getInputStream())useDelimiter(\\\\ A)。            的System.out.println(命令:+ CMD);
            的System.out.println(执行打印数据);            而(s.hasNext()){
                的System.out.println(s.next());
            }
        }赶上(IOException异常前){            。Logger.getLogger(LoudnessEqualizationToggle.class.getName())日志(Level.SEVERE,空,前);
        }
    }

一切工作正常。我得到的输出到命令提示符:


  

操作成功完成


所以我开始的注册表编辑器的验证是否值已更改,但让我吃惊的什么都没有改变。值是和以前一样。

我是否有权限?是结果
我用一个批处理文件来执行我的应用程序的以管理员身份运行

批处理文件:

  CD%〜DP0
Java的罐子LoudnessEqualizationToggle.jar
暂停

从创建快捷方式,并以管理员身份运行。

你是不是关键的所有者?是

我测试了我的命令和权限与如果当前值 0 在Windows注册表可以更改为 1 预期。

该测试批处理文件是:

  CD%〜DP0
REG ADD \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\MMDevices\\Audio\\Render\\{d348b8e8-3118-4a9c-9b43-422647b555ca}\\FxProperties\" / F / V{E0A941A0-88A2-4df5-8D6B-DD20BB06E8FB},4/吨REG_DWORD / d的1
净停止audiosrv //刚刚重启音频到这里,并不重要
净停止AudioEndpointBuilder
NET START audiosrv
NET START AudioEndpointBuilder
暂停

的值已在Windows注册表改变批处理文件执行之后从0到1(所需的操作)。

所以,当我从命令提示符窗口中直接运行我的指挥,我可以看到改变的价值。但是,当我在我的Java应用程序中运行,从运行EXEC我的指挥,没有值在Windows注册表中更改,但我看到的命令没有问题的执行。而就我的Java调试我看到正在执行完全一样的命令,我在批处理文件。

什么可能是没有得到通过我的Java应用程序中执行命令REG.EXE更改注册表值的原因?

进程监视器屏幕(由渔业部小费后):

进程监视器日志,过滤器设置为包括路径MMDevices

解决方案

WOW6432Node 在进程监视器日志表示Java code与32位Java执行在Windows 64 32位的环境。因此,所谓的 REG.EXE 也是32位的%SystemRoot%\\ Syswow64资料\\ REG.EXE 因此这修改在 WOW6432Node DWORD值。

有关Windows在Windows上的细节微软的文章:

一个解决方案,32位和64位Java工作的32位和64位的Windows将是:


  1. 获取字符串值的环境变量 SYSTEMROOT


  2. 检查文件 SYSTEMROOT +\\\\ \\\\ Sysnative REG.EXE存在。

    这是仅适用于Windows x64上运行32位Java的情况。在其他情况下,即在Windows 64或Windows x86 32位Java的64位Java,该文件不存在,因为 Sysnative 不存在。是的, Sysnative 不适用于在Windows x64的64位应用程序存在。


  3. 如果文件 REG.EXE Sysnative 存在于Windows目录下,调用 SYSTEMROOT +\\\\ \\\\ Sysnative REG.EXE


  4. 否则叫 SYSTEMROOT +\\\\ System32下\\\\ REG.EXE这是正确的可执行64位Java在Windows 64以及32比特的Java在Windows 86。


I'm trying to change registry value from my Java application. I use the command:

LoudnessEqualizationToggle.execCmdNoReturn("reg add \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\MMDevices\\Audio\\Render\\"+guid+"\\FxProperties\""
                                           + " /f /v \"{E0A941A0-88A2-4df5-8D6B-DD20BB06E8FB},4\" /t REG_DWORD /d \""+((activateLEOnKey) ? 1 : 0)+"\"");

Where guid is {d348b8e8-3118-4a9c-9b43-422647b555ca}

activateLEOnKey is boolean value

execCmdNoReturn function:

public static final void execCmdNoReturn(String cmd)  {

        try {
            Scanner s = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream()).useDelimiter("\\A");

            System.out.println("Command:"+cmd);
            System.out.println("Printing executed data");

            while (s.hasNext()) {
                System.out.println(s.next());
            }
        } catch (IOException ex) {

            Logger.getLogger(LoudnessEqualizationToggle.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Everything works okay. I get output into command prompt:

The operation completed successfully

So I start regedit to verify if value has changed, but to my surprise nothing changed. The value is the same as before.

Do I have permissions? Yes
I use a batch file to execute my application with Run as administrator.

Batch file:

cd %~dp0
java -jar LoudnessEqualizationToggle.jar
pause

Created shortcut from that and run as administrator.

Are you owner of that key? Yes

I tested my command and permissions with a batch file testing if the current value 0 in Windows registry can be changed to 1 as expected.

Batch file for that test is:

cd %~dp0
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\{d348b8e8-3118-4a9c-9b43-422647b555ca}\FxProperties" /f /v "{E0A941A0-88A2-4df5-8D6B-DD20BB06E8FB},4" /t REG_DWORD /d "1"
net stop audiosrv //just restarting audio down here , not important
net stop AudioEndpointBuilder
net start audiosrv
net start AudioEndpointBuilder
pause

The value has changed in Windows registry after batch file execution from 0 to 1 (desired action).

So when I run my command from command prompt window directly, I can see the value changed. But when I run my command from runtime exec within my Java application, no value is changed in Windows registry, although I see that the command executed with no problem. And on my Java debug I see EXACTLY the same command being executed as I have in batch file.

What could be the reason for not getting the registry value changed by reg.exe command executed from within my Java application?

Process Monitor screen (after a tip by Mofi):

解决方案

WOW6432Node in Process Monitor log indicates that the Java code is executed with 32-bit Java in a 32-bit environment on Windows x64. Therefore the called reg.exe is also the 32-bit %SystemRoot%\SysWOW64\reg.exe which modifies therefore the DWORD value in WOW6432Node.

Microsoft articles with details about Windows On Windows:

One solution working for 32-bit and 64-bit Java on 32-bit and 64-bit Windows would be:

  1. Get string value of environment variable SystemRoot.

  2. Check if file SystemRoot + "\\Sysnative\\reg.exe" exists.

    This is only the case for 32-bit Java executed on Windows x64. In all other cases, i.e. 64-bit Java on Windows x64 or 32-bit Java on Windows x86, this file does not exist because Sysnative does not exist. Yes, Sysnative does not exist for 64-bit applications on Windows x64.

  3. If the file reg.exe exists in Sysnative in Windows directory, call SystemRoot + "\\Sysnative\\reg.exe".

  4. Otherwise call SystemRoot + "\\System32\\reg.exe" which is the right executable for 64-bit Java on Windows x64 as well as 32-bit Java on Windows x86.

这篇关于改变与getRuntime()注册表项值。EXEC(CMD)不更改注册表值,甚至是操作成功结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆