Java Utililty Methods Host Name Get

List of utility methods to do Host Name Get

Description

The list of methods to do Host Name Get are organized into topic(s).

Method

InetAddressgetHostGateway()
get Host Gateway
@SuppressWarnings("unused")
String _gateway = null, _ip = null;
if (System.getProperty("os.name").startsWith("Windows")) {
    return parseWindows();
try {
    BufferedReader reader = new BufferedReader(new FileReader("/proc/net/route"));
    String line;
...
StringgetHostInformation()
Return a string identifiying this host/user/JVM.
String hostname = "unknown";
final InetAddress host;
try {
    host = InetAddress.getLocalHost();
    hostname = host.getHostName();
    hostname = hostname.replaceAll("\\..*", "");
} catch (Exception ignore) {
final String userName = System.getProperty("user.name");
return userName + "@" + hostname + " " + (getPid() != -1 ? "(" + getPid() + ")" : "");
StringgetHostMac(String host)
get Host Mac
try {
    NetworkInterface network = NetworkInterface.getByInetAddress(InetAddress.getByName(host));
    byte[] mac = network.getHardwareAddress();
    StringBuilder sb = new StringBuilder();
    for (int i = 0, ci = mac.length; i < ci; i++) {
        sb.append(String.format("%02X%s", mac[i], (i < ci - 1) ? "-" : ""));
    return sb.toString();
...
StringgetHostName()
get Host Name
String hostName = null;
try {
    InetAddress a = InetAddress.getLocalHost();
    hostName = a.getHostName();
} catch (UnknownHostException e) {
if (hostName == null) {
    boolean bFindIP = false;
...
StringgetHostname()
Return this machine's hostname, or "unknown" if unable to retrieve the name.
try {
    java.net.InetAddress addr = java.net.InetAddress.getLocalHost();
    return addr.getHostName();
} catch (java.net.UnknownHostException ex) {
    return "unknown";
StringgetHostName()
get Host Name
try {
    InetAddress.getLocalHost().getHostName();
    return "0.0.0.0";
} catch (UnknownHostException e) {
    e.printStackTrace();
    return "127.0.0.1";
StringgetHostName()
Gets the host name.
try {
    final java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
    return localMachine.getHostName();
} catch (final UnknownHostException e) {
    return "localhost";
StringgetHostname()
Returns the hostname
if (null == _hostname) {
    try {
        _hostname = InetAddress.getLocalHost().getHostName();
    } catch (Exception e) {
        _hostname = "UNKNOWN";
return _hostname;
...
StringgetHostName()
Returns a hostname from the current host

TODO: Platform dependent

return System.getenv().containsKey("HOSTNAME")
        ? Iterables.getFirst(DOT_SPLITTER.split(System.getenv("HOSTNAME")), getInetHost())
        : getInetHost();
StringgetHostName()
get Host Name
InetAddress addr;
try {
    addr = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
    return "localhost";
return addr.getHostName();