Sunday, 6 March 2022

Java: Get the name of system or host

Below snippet print the name of system.

public static String getLocalHostName() throws UnknownHostException {
    InetAddress localhost = InetAddress.getLocalHost();
    return localhost.getHostName();
}

 

Find the below working application.

 

LocalHostNameDetails.java

package com.sample.app.net;

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

public class LocalHostNameDetails {
    
    public static String getLocalHostName() throws UnknownHostException {
        InetAddress localhost = InetAddress.getLocalHost();
        return localhost.getHostName();
    }
    
    public static void main(String[] args) throws UnknownHostException {
        System.out.println(getLocalHostName());
    }

}

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment