Monday, December 8, 2025
HomeLanguagesJavaJava Program to Get System Name for Windows and Linux Machine

Java Program to Get System Name for Windows and Linux Machine

We can get the System name for a Windows or Linux machine using the getHostName() method of the InetAddress class of java.net package after getting the IP address of the system using getLocalHost() method of the same class. The class InetAddress gets the IP address of any hostname.  getLocalHost() method of the InetAddress class gets the address of the local host. getHostName() gets the hostname for a given IP address or returns the textual representation of the IP address if the operation is not allowed by the security manager.

Syntax

public String getHostName()

Returns: The address of the host for a given IP address or the textual representation of the IP address if the operation is not allowed.

Example:

Java




// Java program to demonstrate getting
// the System name of the user
 
import java.net.InetAddress;
public class GFG {
    public static void main(String[] args)
    {
        try {
 
            // get system name
            String SystemName
                = InetAddress.getLocalHost().getHostName();
 
            // SystemName stores the name of the system
            System.out.println("System Name : "
                               + SystemName);
        }
        catch (Exception E) {
            System.err.println(E.getMessage());
        }
    }
}


 

 

Output:

 

getSystemName

 

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32429 POSTS0 COMMENTS
Milvus
103 POSTS0 COMMENTS
Nango Kala
6803 POSTS0 COMMENTS
Nicole Veronica
11947 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12015 POSTS0 COMMENTS
Shaida Kate Naidoo
6936 POSTS0 COMMENTS
Ted Musemwa
7190 POSTS0 COMMENTS
Thapelo Manthata
6884 POSTS0 COMMENTS
Umr Jansen
6872 POSTS0 COMMENTS