Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find out the local machine name using a java program?

 
rajesh madhira
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I want to find out the machine name by using a java program. Can somebody help me out?

Thanks
Java_learner
 
Srikanth Ramu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

should get the localhost name
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome to the Ranch, Rajesh Madhira.

I couldn't get InetAddress.getLocalHost().getHostName() to work; it threw an exception. I tried InetAddress.getByName("localhost") and got the result of localhost/127.0.0.1,

I should have known that result before trying!
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bit difficult, this one. You can also try: -

InetAddress.getByName(addr.getHostAddress()).getHostName()

You can also use NetworkInterface.getNetworkInterfaces() then call getInetAddresses() on each NetworkInterface object, then try some InetAddress methods on those ...
 
rajesh madhira
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everyone ..Just to add the following code worked fine for me.

import java.net.*;

public class Test {

public static void main(String args[]) {
try {
String name = InetAddress.getLocalHost().getHostName();
System.out.println(name);
} catch(Exception ex) { ex.printStackTrace();}
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic