macos - 使用 java 的命令行方法进行浏览器版本检测
问题描述
我知道使用在我的系统上正常工作的命令行在
mac中获取 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 程序中引发错误。
无法运行程序 "/Application/"Google" 错误=2 没有这样的文件或目录
我尝试将查询转义为/Applications/\"Microsoft Edge.app\"/Contents/MacOS/\"Microsoft Edge\" -version
。我在作为查询传递之前打印相同的查询,如果我直接将其复制粘贴到终端上,我将得到完全相同的查询。
下面是代码片段
我错过了什么吗?
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();
解决方案
根据相关api Runtime.getRuntime().exec(String command),需要知道传入的参数是合法的 String,所以需要注意String参数类型的格式。
编辑:
我重现了您的问题并遇到了同样的问题。经过搜索,我发现了同样的问题。这是因为 Process 不同于终端进程。您必须添加-l
以登录用户身份运行命令。
推荐阅读
- node.js - Nestjs 7 升级类验证器问题
- excel - Excel/GSheets 计算动态列表中的唯一值与重复值
- r - ggplot2中注释中的逗号和撇号
- firebase - 如何修复“未处理的异常:错误状态:无法在 DocumentSnapshotPlatform 上获取不存在的字段”?
- ios - UITextView 在横向模式下不滚动(Swift)
- php -
在这个项目中,我将我的 html 表单发送到 PHP。我将 Xamp 用于 PHP 方面,但在从表单接收信息后,我的 PHP 显示出现问题。我使用了验证器,问题必须在我的代码中处理 <?PHP。我听说您应该在 <?PHP 之外包含,但我不完全确定。更不用说,如果你们想让我包含我的 htm
- vb.net - 如何将列表框字符串显示到另一个列表框
- node.js - 如何使用 ElasticSearch.js 在 ElasticSearch 中按字符串日期排序
- azure-active-directory - Microsoft Graph API:获取分配给企业应用程序的用户
- xcode - 无法在终端 macOS 10.15.7 上安装 cocoapods