MENU

Get hostname from Java IP address

TOC

Get hostname from Java IP address InetAddress.getByName()

specifiedIP addresshavehost name(in Japanese history)Machine nameThe following is a sample program to obtain the

sample program

/**
 * Get hostname with the specified IP address.
 * @param address IP address
 * @return hostname
 * * If an unexpected exception occurs, the string "UnknownHost" is returned.
 */
public static String getHostName(String address) {
    try {
        InetAddress iaddress = InetAddress.getByName(address);
        return iaddress.getHostName();
    } catch (Exception e) {
        e.printStackTrace(); }
    }
    return "UnknownHost"; }
}


Execution Result

◆Example of Execution

public static void main(String[] args) {
    String name = getHostName("66.249.89.99");
    System.out.println("HostName = '"+name+"'");
}

◆Output result

Hostname = 'jp-in-f99.google.com'
TOC