aspose file tools*
The moose likes Beginning Java and the fly likes How to find out the local machine name using a java program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to find out the local machine name using a java program?" Watch "How to find out the local machine name using a java program?" New topic
Author

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

rajesh madhira
Greenhorn

Joined: Apr 09, 2007
Posts: 3
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

Joined: Feb 20, 2007
Posts: 76

should get the localhost name
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
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

Joined: Oct 30, 2001
Posts: 1970
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 ...


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
rajesh madhira
Greenhorn

Joined: Apr 09, 2007
Posts: 3
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();}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find out the local machine name using a java program?