如何使用 Java 应用程序在 Mac OS X 上打开 System Profiler.app?

发布于 2024-10-27 17:38:26 字数 370 浏览 4 评论 0原文

我必须从 Java 应用程序显示 Mac OS X 的系统信息。

我创建了一个按钮并尝试执行以下操作:

private static String[] sysInfoPathMac =
    {"/usr/bin/open", "'/Applications/Utilities/System Profiler.app'"};

在按钮的单击事件中,我执行以下命令:

Runtime.getRuntime().exec(sysInfoPathMac);

单击按钮后我没有得到任何内容。它不会抛出任何错误,也不运行系统分析器。

我到底做错了什么以及如何完成它?

I have to show system information for my Mac OS X from my Java application.

I have created a button and trying to execute following:

private static String[] sysInfoPathMac =
    {"/usr/bin/open", "'/Applications/Utilities/System Profiler.app'"};

In the click event of the button I execute following command:

Runtime.getRuntime().exec(sysInfoPathMac);

I am not getting anything once button is clicked. It does not throw any error and neither does it run the system profiler.

What exactly I am doing wrong and how to get it done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灼痛 2024-11-03 17:38:26

删除单引号。当您从 shell 运行该命令时,需要使用引号,因为参数中包含空格。从 Java 启动该进程时这不是问题,因为数组的每个元素都被视为单独的参数,无论它是否包含空格。

String[] command = {"/usr/bin/open", "/Applications/Utilities/System Profiler.app"};
Process proc = Runtime.getRuntime().exec(command);
int resultCode = proc.waitFor();
if (resultCode != 0) {
    throw new Exception("failed to open system profiler");
}

请注意,通常在使用 Runtime.exec() 时,您需要使用 stdout 和 stderr 流以避免死锁(尽管在本例中这不是问题,因为“open”命令不会产生太多输出)。有关该主题的更多信息,请查看 Michael C. Daconta 的文章“When Runtime.exec() Won't”
http://www.javaworld.com/javaworld/jw -12-2000/jw-1229-traps.html

Remove the single-quotes. When you run the command from the shell the quotes are needed because the argument has a space in it. This isn't an issue when starting the process from Java because each element of the array is treated as a separate argument regardless of whether it contains spaces.

String[] command = {"/usr/bin/open", "/Applications/Utilities/System Profiler.app"};
Process proc = Runtime.getRuntime().exec(command);
int resultCode = proc.waitFor();
if (resultCode != 0) {
    throw new Exception("failed to open system profiler");
}

Note that in general when using Runtime.exec() you'll want to consume the stdout and stderr streams to avoid deadlock (although in this case it's not an issue since the 'open' command doesn't produce much output). For more on the subject check out Michael C. Daconta's article "When Runtime.exec() Won't"
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文