How do I get a localhost hostname?

package org.kodejava.network;

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

public class HostNameExample {
    public static void main(String[] args) {
        try {
            InetAddress address = InetAddress.getLocalHost();

            System.out.println("Hostname: " + address.getHostName());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

An example of output produce by the code snippet above is:

Hostname: Krakatau
Wayan

Leave a Reply

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