Get hostname from IP address

This is an example of how to get the host name of a remote host from its IP address. Doing hostname resolution from IP addresses implies that you should :

  • Retrieve the Address object for the specified host that contains all address related information about the specific host
  • Use the getHostName() and/or getCanonicalHostName() API methods to retrieve the hostname and/or canonical hostname of the specific host
  • as described in the code snippet below.

If the specific host address exists and there is no connectivity issues between the client and the host machines then you should be able to get the hostname from the designated host IP address.

package com.javacodegeeks.snippets.core;


public class GetHostnameFromIPAddress {
	public static void main(String[] args) {
		try {
			InetAddress inetAddr = InetAddress.getByName("");

		    // Get the host name
		    String hostname = inetAddr.getHostName();

		    // Get canonical host name
		    String canonicalHostname = inetAddr.getCanonicalHostName();
			System.out.println("Hostname: " + hostname);
			System.out.println("Canonical Hostname: " + canonicalHostname);
		catch (UnknownHostException e) {
			System.out.println("Host not found: " + e.getMessage());


This was an example of how to get the hostname from the IP address of a specific host in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Naufal Syarifuddin
Naufal Syarifuddin
5 years ago

i want to ask i have ip address but after i tried to convert to device name cannot retruning device name only returning ip address can you help me for doing it thanks

Back to top button