Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在windows中使用java查找特定注册表项_Java_Windows_Registry_Version - Fatal编程技术网

在windows中使用java查找特定注册表项

在windows中使用java查找特定注册表项,java,windows,registry,version,Java,Windows,Registry,Version,我可以在windows中进入regedit,然后进入edit->find并键入我想在我的计算机中找到的密钥(在我的情况下是Maxima),并找到所需的密钥(在我的情况下是在“HKEY\U LOCAL\U MACHINE\SOFTWARE\Wow6432Node\Microsoft\windows\CurrentVersion\Uninstall\Maxima-5.17.1\U is1”中) 但我需要在java中动态执行此操作,并在windows中查找安装的maxima的当前版本及其目录位置。我不

我可以在windows中进入regedit,然后进入edit->find并键入我想在我的计算机中找到的密钥(在我的情况下是Maxima),并找到所需的密钥(在我的情况下是在“HKEY\U LOCAL\U MACHINE\SOFTWARE\Wow6432Node\Microsoft\windows\CurrentVersion\Uninstall\Maxima-5.17.1\U is1”中) 但我需要在java中动态执行此操作,并在windows中查找安装的maxima的当前版本及其目录位置。我不知道如何进行


我尝试使用这里所述的方法,但要使用这些方法,我需要知道密钥。如何在java中动态查找密钥?或者有没有其他方法可以使用java在windows中查找软件的版本和目录位置?

我将使用类作为您的答案。因为它是用纯java代码编写的

  • 拥有来自的
    WinRegistry
  • 获取父项中所有项的列表
  • 筛选列表以获取最合适的密钥(或精确密钥)
  • 然后可以检查此键中所需的值
  • 以下是帮助您的代码:

    List<String> ls = WinRegistry.readStringSubKeys(WinRegistry.HKEY_LOCAL_MACHINE,
        "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\");
    String key = ls.stream().filter(st -> st.matches("Maxima.*")).findAny().get();
    
    List ls=WinRegistry.readStringSubKeys(WinRegistry.HKEY\u LOCAL\u MACHINE,
    “软件\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\”;
    String key=ls.stream().filter(st->st.matches(“Maxima.*)).findAny().get();
    

    现在,这个
    值将是
    Maxima-5.17.1_is1
    (如果存在,否则将抛出
    java.util.NoTouchElementException
    )。您可以使用它来获取任何

    我会避免强制访问私有方法,因为:

  • 在Java的未来版本中,它们可能不存在。实际上,下一次小更新可能没有这些方法
  • 如果代码只能在没有SecurityManager的情况下工作,那么它的可移植性就比较差
  • 如果使用,您的代码保证在所有Java版本中都能工作,至少在Microsoft在Windows中包含reg.exe的情况下:

    ProcessBuilder builder = new ProcessBuilder("reg", "query",
        "HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
    Process reg = builder.start();
    try (BufferedReader output = new BufferedReader(
        new InputStreamReader(reg.getInputStream()))) {
    
        Stream<String> keys = output.lines().filter(l -> !l.isEmpty());
        Stream<String> matches = keys.filter(l -> l.contains("\\Maxima"));
        Optional<String> key = matches.findFirst();
        // Use key ... 
    }
    reg.waitFor();
    
    ProcessBuilder=新的ProcessBuilder(“注册”、“查询”,
    “HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall”);
    Process reg=builder.start();
    try(BufferedReader输出=新的BufferedReader(
    新的InputStreamReader(reg.getInputStream())){
    流键=output.lines().filter(l->!l.isEmpty());
    流匹配=键。过滤器(l->l.contains(\\最大值”);
    可选键=matches.findFirst();
    //使用键。。。
    }
    注册waitFor();
    
    不重复的可能重复项。我尝试了上面提到的方法,但“要使用这些方法,我需要知道密钥”使用
    hKey=WinRegistry.hKey\u LOCAL\u MACHINE
    key=“SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\Uninstall\\Maxima-5.17.1\u is1”
    。注册表项是一个简单的字符串,包含
    HKEY\u xxx
    之后的所有内容。因此,动态设置包含maxima某些版本信息的密钥应该没有问题。如何在不硬编码的情况下动态查找密钥?我的意思是,在这种情况下,我必须去RegEdit并搜索maxima来找到它的位置。我希望在java中实现这一点,而不必手动转到regedit。请记住,访问私有方法可能最终会中断。在面向对象的API(如JavaSE)中,公共方法永远不会改变,但私有方法是一种实现细节,在将来的任何版本中都可能改变。