Home » Core Java » net » InetAddress » Get hostname from IP address

About Ilias Tsagklis

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

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;

import java.net.InetAddress;
import java.net.UnknownHostException;

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.

(No Ratings Yet)
1 Comment Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Naufal Syarifuddin Recent comment authors
newest oldest most voted
Notify of
Naufal Syarifuddin
Naufal Syarifuddin

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