【问题标题】:Browser Version Detection using Command line method using java使用 java 的命令行方法进行浏览器版本检测
【发布时间】:2021-08-17 17:17:29
【问题描述】:

我知道使用在我的系统上正常运行的命令行在 ma​​c 中获取 Edge、Firefox 和 Chrome 浏览器版本的命令。 Edge=/Applications/"Microsoft Edge.app"/Contents/MacOS/"Microsoft Edge" -version FireFox=/Applications/Firefox.app/Contents/MacOS/firefox -v Chrome=/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version

Chrome1=/Applications/"Google Chrome.app"/Contents/MacOS/"Google Chrome" --version

当我尝试使用 Firefox 时,它成功地返回了 Firefox 版本。 但是当我尝试使用 Edge 或 chrome 时,它​​会在 java 程序中引发错误。

Cannot run program "/Application/"Google" error=2 no such file or directory

我尝试将查询转义为/Applications/\"Microsoft Edge.app\"/Contents/MacOS/\"Microsoft Edge\" -version。我在作为查询传递之前打印相同的查询,如果我直接将它复制粘贴到终端上,我会得到完全相同的查询。

下面是代码sn-p

我错过了什么吗?

    String line;
            Process process;
            String msEdgeVersion = null;
    
            /*process = Runtime.getRuntime().exec("/Applications/Firefox.app/Contents/MacOS/firefox -v");*/
    
            process = Runtime.getRuntime().exec(/Applications/"Microsoft Edge.app"/Contents/MacOS/"Microsoft Edge" -version);
    
            // process = Runtime.getRuntime().exec("where notepad");
    
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((line = br.readLine()) != null) {
                if (!StringUtils.isBlank(line)) {
                    msEdgeVersion = line;
                    System.out.println(line);
                }
            }
            process.waitFor();
            process.destroy();

【问题讨论】:

    标签: macos google-chrome firefox command-line microsoft-edge


    【解决方案1】:

    根据相关apiRuntime.getRuntime().exec(String command)需要知道传入的参数是有效的String,所以需要注意String参数类型的格式。 p>

    编辑

    我重现了您的问题并遇到了同样的问题。经过搜索,我找到了same problem。这是因为 Process 不同于终端进程。您必须添加-l 才能以登录用户身份运行命令。

    这是我的测试:

    【讨论】:

    • 感谢您的回复,但我已经尝试过问题中提到的此查询。它会引发同样的错误。
    • 好吧,我只关注你的代码,忽略了你描述的。我已经修改了答案,我想这对你有帮助。
    • 请问您是否有机会检查我的答案?如果您有任何其他问题,我很乐意为您提供帮助。
    • 感谢您的回复,我使用传递字符串数组解决了这个问题,并在命令中进行了少量修改。字符串命令 =/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge -version ; int lastSpaceIndex = command.lastIndexOf(" "); String[] commandArray = new String[]{command.substring(0, lastSpaceIndex),command.substring(lastSpaceIndex + 1)};我将 commandArray 传递给对我有用的 exec()。
    • 很高兴得知您的问题已得到解决。如果可能,您可以将其发布为答案,这也可能对社区的其他成员有所帮助。
    猜你喜欢
    • 2011-03-26
    • 2023-03-15
    • 2014-02-11
    • 2012-10-18
    • 2016-12-11
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多