1

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
tshepang
  • 12,111
  • 21
  • 91
  • 136
p0tta
  • 1,461
  • 6
  • 28
  • 49
  • possible duplicate of [java runtime.getruntime() getting output from executing a command line program](http://stackoverflow.com/questions/5711084/java-runtime-getruntime-getting-output-from-executing-a-command-line-program) – Bohemian May 04 '12 at 00:15

2 Answers2

1

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();
Martin
  • 7,089
  • 3
  • 28
  • 43
1

Try the following code:

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

user845279
  • 2,794
  • 1
  • 20
  • 38