Java
Coding Tips
Programming
Hostname
Technology

How to get the hostname in Java

Codemia Knowledge Hub ·

The usual way is InetAddress.getLocalHost().getHostName() from java.net. It needs no extra dependencies, but it relies on DNS and can return unexpected values, so an environment variable (HOSTNAME on Unix, COMPUTERNAME on Windows) is a common fallback.

InetAddress.getLocalHost()

java
1import java.net.InetAddress;
2
3public class Main {
4    public static void main(String[] args) {
5        try {
6            InetAddress ip = InetAddress.getLocalHost();
7            String hostname = ip.getHostName();
8            System.out.println("Hostname: " + hostname);
9        } catch (Exception e) {
10            e.printStackTrace();
11        }
12    }
13}

InetAddress.getLocalHost() returns the InetAddress for the local host, and getHostName() gives its hostname.

Advantages:

  • Only a few lines of code.
  • Built-in Java API, no additional dependencies.

Drawbacks:

  • It relies on the system's DNS resolution.
  • In some configurations, especially complex corporate networks, getLocalHost() may return unexpected values such as localhost or 127.0.0.1.

Reading the hostname from environment variables

java
1public class Main {
2    public static void main(String[] args) {
3        String hostname = System.getenv("HOSTNAME");
4        if (hostname == null) {
5            // Fallback to another property or method
6            hostname = System.getenv("COMPUTERNAME");
7        }
8        System.out.println("Hostname: " + hostname);
9    }
10}

Advantages:

  • Reads the value directly.
  • Easy to chain fallbacks when one variable is missing.

Drawbacks:

  • Platform dependent: the variable name differs by OS (HOSTNAME on Unix-based systems, COMPUTERNAME on Windows).
  • Users or the system can modify environment variables, which affects reliability and can be a security risk.
MethodProsCons
InetAddressSimple to use; No extra dependenciesDepends on DNS; May return loopback address
System PropertiesDirect; Can have fallbacksPlatform dependent; Security concerns

Choosing and testing an approach

  • Security: network and environment data can be tampered with, through DNS spoofing or edited environment variables.
  • Performance: InetAddress.getLocalHost() involves name resolution, so it can be slower and less predictable.
  • Fallbacks: environment variables do not reliably exist on every platform, so implement a fallback.
  • Testing: test in every environment you deploy to, particularly containers and cloud hosts, where OS and network configuration can differ significantly from a workstation.

InetAddress is generally recommended for its simplicity. Handle the edge cases and fall back when the first attempt fails.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms