how to get hostname from ip address in java?

The java.net.InetAddress class represents an IP address. It provides the methods to work with IP and host name.

Get hostname from ip address in java

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.w3schools;
import java.net.InetAddress;
public class NetworkTest {
public static void main(String args[]){
try {
InetAddress host = InetAddress.getByName("107.180.2.128");
System.out.println(host.getHostName());
} catch(Exception e) {
e.printStackTrace();
}
}
}
package com.w3schools; import java.net.InetAddress; public class NetworkTest { public static void main(String args[]){ try { InetAddress host = InetAddress.getByName("107.180.2.128"); System.out.println(host.getHostName()); } catch(Exception e) { e.printStackTrace(); } } }
package com.w3schools;

import java.net.InetAddress;

public class NetworkTest {
  public static void main(String args[]){
	try {
	    InetAddress host = InetAddress.getByName("107.180.2.128");
            System.out.println(host.getHostName());
        } catch(Exception e) {
            e.printStackTrace();
        }
  }
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ip-107-180-2-128.ip.secureserver.net
ip-107-180-2-128.ip.secureserver.net
ip-107-180-2-128.ip.secureserver.net

Related topics