Get HostName in Java

Muhammad Zeeshan Jan 30, 2023 Mar 20, 2022
  1. Use InetAddress to Get HostName in Java
  2. Demonstrate Using the InetAddress Class to Get HostName in Java
Get HostName in Java

In this tutorial, we’ll look at how to obtain the IP Address and HostName using the Java API.

Use InetAddress to Get HostName in Java

Package java.net contains the class InetAddress that deals with the current machine’s IP address and hostname.

By gathering the system’s IP address with the same class’s getLocalHost() function, we can get the system name for a Windows computer using getHostName() of the InetAddress class in the java.net package.

If the security manager does not authorize getting the address of the localhost, getHostName() delivers the textual representation of the IP address.

The getLocalHost() and getByName (String host) methods of the InetAddress class deal with local and remote hosts, respectively.

Syntax:

InetAddress inetadd = InetAddress.getLocalHost();

This function will create an instance of InetAddress for the inetadd local server, where the Java program is running. As a result, the address of the localhost is returned.

This is done by retrieving the host’s name from the system and transforming it into InetAddress.

Demonstrate Using the InetAddress Class to Get HostName in Java

Firstly, import the following library.

java.net.InetAddress

We’ll show you how to retrieve hostname in Java using a simple example.

InetAddress inetadd = InetAddress.getLocalHost();

We’ll use the InetAddress method to create an instance of InetAddress for the local server named inetadd, where the Java program operates.

String name = inetadd.getHostName();

We’ll obtain the hostname using the getHostName() method and save it in a string type variable named name.

String address = inetadd.getHostAddress();

After that, with the help of the getHostAddress() method, we’ll get the host address and save it in a string type variable named address. If this could not resolve the local or site hostname into an address, UnknownHostException will be thrown.

Source code:

import java.net.InetAddress;
import java.net.UnknownHostException;
public class HostnameExample {
    public static void main(String[] args)
    {
        try {
            InetAddress inetadd = InetAddress.getLocalHost();
            String name = inetadd.getHostName();
            String address = inetadd.getHostAddress();
            System.out.println("HostName is : "+name);
            System.out.println("Host Address is: "+address);
        }
        catch(UnknownHostException u){
        }
}
}

Output:

$javac HostnameExample.java
$java -Xmx128M -Xms16M HostnameExample
HostName is : f97aedb6192a
Host Address is: 127.0.0.2
Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn