macos 从JAVA问题执行Unix系统命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7200307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute Unix system command from JAVA problem
提问by Ajith
I am facing a weird issue with executing a system command from JAVA code.
Actually i want to get the Mac OSX
system information from my JAVA App.
For that im using
我在从 JAVA 代码执行系统命令时遇到了一个奇怪的问题。
其实我想Mac OSX
从我的 JAVA 应用程序中获取系统信息。
为此,我正在使用
Runtime.getRuntime().exec("system_profiler -detailLevel full");
Runtime.getRuntime().exec("system_profiler -detailLevel full");
This is working fine.If i print the output,it is cool.
But i want to write this information to a plist
file for future use.For that im using the -xml argument of system_profiler
.like,
这工作正常。如果我打印输出,它很酷。但我想将此信息写入plist
文件以备将来使用。system_profiler
为此,我使用.like的 -xml 参数,
String cmd = "system_profiler -detailLevel full -xml > "+System.getProperty( "user.home" )+"/sysinfo.plist";
Process p = Runtime.getRuntime().exec(cmd);
Basically this should create a plist file in the current users home directory.
基本上这应该在当前用户的主目录中创建一个 plist 文件。
But this seems to be not writing anything to file.
但这似乎没有向文件写入任何内容。
Am i missing something here ?
我在这里错过了什么吗?
回答by Christian.K
My Java is more than rusty, so please be gentle. ;-)
我的 Java 不仅仅是生锈,所以请保持温和。;-)
Runtime.exec()
does not automatically use the shell to execute the command you passed, so the IO redirection is not doing anything.If you just use:
"/bin/sh -c system_profiler -detailLevel full > path/file.plist"
Then the string will be tokenized into:
{ "/bin/sh", "-c", "system_profiler", "-detailLevel", "full", ">", "path/file.plist" }
Which also wouldn't work, because
-c
only expects a single argument.
Runtime.exec()
不会自动使用 shell 来执行您传递的命令,因此 IO 重定向没有做任何事情。如果您只是使用:
"/bin/sh -c system_profiler -detailLevel full > path/file.plist"
然后字符串将被标记为:
{ "/bin/sh", "-c", "system_profiler", "-detailLevel", "full", ">", "path/file.plist" }
这也行不通,因为
-c
只需要一个参数。
Try this instead:
试试这个:
String[] cmd = { "/bin/sh", "-c", "system_profiler -detailLevel full > path/file.plist" };
Process p = Runtime.getRuntime.exec(cmd);
Of course, you could also just read the output of your Process
instance using Process.getInputStream()
and write that into the file you want; thus skip the shell, IO redirection, etc. altogether.
当然,您也可以使用读取Process
实例的输出Process.getInputStream()
并将其写入所需的文件;因此完全跳过shell,IO重定向等。
回答by paulsm4
Christian.K is absolutely correct. Here is a complete example:
Christian.K 是绝对正确的。这是一个完整的例子:
public class Hello {
static public void main (String[] args) {
try {
String[] cmds = {
"/bin/sh", "-c", "ls -l *.java | tee tmp.out"};
Process p = Runtime.getRuntime().exec (cmds);
p.waitFor ();
System.out.println ("Done.");
}
catch (Exception e) {
System.out.println ("Err: " + e.getMessage());
}
}
}
If you weren't using a pipe (|
) or redirect (>
), then you'd be OK with String cmd = "ls -l *.java"
, as in your original command.
如果您没有使用管道 ( |
) 或重定向 ( >
),那么您可以使用String cmd = "ls -l *.java"
,就像在您的原始命令中一样。
If you actually wanted to seeany of the output in your Java console window, then you'd ALSO need to call Process.getInputStream()
.
如果您真的想在 Java 控制台窗口中看到任何输出,那么您还需要调用Process.getInputStream()
.
Here's a good link: Running system commands in Java applications
这是一个很好的链接: 在 Java 应用程序中运行系统命令