Executing a command on the command line and readin

2019-07-28 22:49发布

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

2条回答
forever°为你锁心
2楼-- · 2019-07-28 23:16

Try the following code:

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

查看更多
Evening l夕情丶
3楼-- · 2019-07-28 23:25

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();
查看更多
登录 后发表回答