Find the Hostname of a Server Using the IP Address

Find the Hostname of a Server Using the IP Address

The package has an API that does the hard work of collecting all the details and returns the server name. It’s easy with the code snippet listed below.

Pass the IP address as an argument and the code will retrieve the hostname if it is valid.

import*;public class FindHostnameUsingIPAddress{   public static void main(String args[])   {      FindHostnameUsingIPAddress findHostnameUsingIPAddress = new FindHostnameUsingIPAddress();      findHostnameUsingIPAddress.proceed(args[0]);   }      private void proceed(String ipAddr)   {      try      {         String hostname = InetAddress.getByName(ipAddr).getHostName();         System.out.println("Hostname of " + ipAddr + " is " + hostname);      }catch(Exception e)      {         System.out.println("Exception: "+e);      }   }}/*

Expected output:

[root@mypc]# java FindHostnameUsingIPAddress of is*/
See also  Java Developer Jobs: Your Guide to Landing the Perfect Position in 2024

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist