如何使用 Java 应用程序在 Mac OS X 上打开 System Profiler.app?
我必须从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除单引号。当您从 shell 运行该命令时,需要使用引号,因为参数中包含空格。从 Java 启动该进程时这不是问题,因为数组的每个元素都被视为单独的参数,无论它是否包含空格。
请注意,通常在使用 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.
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