如何使用Java执行系统命令(linux/bsd)
我试图以便宜的方式在 Java 中执行本地系统命令 (uname -a
)。 我希望获取 uname
的输出并将其存储在字符串中。 这样做的最佳方法是什么? 当前代码:
public class lame {
public static void main(String args[]) {
try {
Process p = Runtime.getRuntime().exec("uname -a");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
}
catch(IOException e1) {}
catch(InterruptedException e2) {}
System.out.println("finished.");
}
}
I am attempting to be cheap and execute a local system command (uname -a
) in Java. I am looking to grab the output from uname
and store it in a String. What is the best way of doing this? Current code:
public class lame {
public static void main(String args[]) {
try {
Process p = Runtime.getRuntime().exec("uname -a");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
}
catch(IOException e1) {}
catch(InterruptedException e2) {}
System.out.println("finished.");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你的方式与我可能做的事情相去不远:
当然,处理你关心的任何异常。
Your way isn't far off from what I'd probably do:
Handle whichever exceptions you care to, of course.
这是最好的方法。
您还可以使用 ProcessBuilder ,它有一个可变参数构造函数,因此您可以节省一两行代码
That is the best way to do it.
Also you can use the ProcessBuilder which has a variable argument constructor, so you could save a line or two of code
你正在做的事情看起来不错。 如果您的命令仅返回单个字符串,则不需要 while 循环,只需将 reader.readLine() 值存储在单个 String 变量中。
另外,你可能应该对这些例外做一些事情,而不是仅仅接受它们。
What you are doing looks fine. If your command is only returning a single string, you don't need the while loop, just store the reader.readLine() value in a single String variable.
Also, you probably should do something with those exceptions, rather than just swallowing them.
我知道这已经很老了,但仍然......
在这里阅读文章:
http://www.javaworld .com/article/2071275/core-java/when-runtime-exec---won-t.html
据我了解,您应该首先读取所执行命令的输出和错误流,然后才
waitFor
进程的返回值。I know this is very old but still...
Reading the article here:
http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html
It is my understanding that you should first read the output and error streams of your executed command and only then
waitFor
the return value of your process.我知道这个问题很老了,但我只是想添加一些可能对某些人有用的信息。
如果您只想从 java 代码运行 uname 命令,最好使用 System 类来获取有关系统的信息。
它不仅可以消除运行终端命令的依赖性,而且还可以独立于操作系统运行。
System Class 可以为您提供以下信息
(来源 - https://www.geeksforgeeks.org/getproperty-and-getproperties-methods-of-system-class-in-java/)
例如:如果我运行的是基于 Linux 的系统,比如说 Ubuntu,以下命令将为我提供有关它的信息。
I know this questionis very old, but I just wanted to add some information that might come handy to some people.
If you just want to run uname command from java code, better use the System class to get information about the system.
It will not only remove the dependency of running the terminal command, but it will also work independently of the Operating System.
System Class can give you the following information
(Source - https://www.geeksforgeeks.org/getproperty-and-getproperties-methods-of-system-class-in-java/)
ex : If I am running a Linux based system, Say Ubuntu, the following command will give me the information about it.