public String getHostName() {
try {
java.net.InetAddress addr = InetAddress.getLocalHost();
return addr.getHostName();
} catch (UnknownHostException ex) {
System.out.println("Error: " + ex.getMessage());
}
return "unknow";
}
Example Demo:
package zexample;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
*
* @author http://titodev.blogspot.com/
*/
public class zExample {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hostname of local machine: " + getHostName());
}
public static String getHostName() {
try {
java.net.InetAddress addr = InetAddress.getLocalHost();
return addr.getHostName();
} catch (UnknownHostException ex) {
System.out.println("Error: " + ex.getMessage());
}
return "unknow";
}
}