在命令行中执行命令和读取的Java控制台输出(Executing a command on the

2019-10-16 16:23发布

我需要使用Java程序我的服务器的主机标识。 有一些是内置的,它可以帮助我做到这一点在Java API中?

lab.com$ hostid
f9y7777j -> How can I get this using java

Answer 1:

下面将允许你运行一个控制台命令,并将结果: -

 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();


Answer 2:

试试下面的代码:

System.out.println(java.net.InetAddress.getLocalHost().getHostName());



文章来源: Executing a command on the command line and reading the console output in Java