在命令行上执行命令并读取Java中的控制台输出 [英] Executing a command on the command line and reading the console output in Java
本文介绍了在命令行上执行命令并读取Java中的控制台输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用Java程序获取服务器的hostId。
I need to get the hostId of my server using a Java program. Is there something in the Java API that is built-in which can help me do this?
lab.com$ hostid
f9y7777j -> How can I get this using java
推荐答案
您可以运行控制台命令并存储结果:-
The following would allow you to run a console command and store the result:-
ProcessBuilder pb = new ProcessBuilder("hostid");
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
{
// Store returned string here.
}
reader.close();
这篇关于在命令行上执行命令并读取Java中的控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文