Implementation:SeleniumHQ Selenium HostIdentifier
| Knowledge Sources | |
|---|---|
| Domains | WebDriver, Infrastructure |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
HostIdentifier provides static, cached methods to resolve the local machine's hostname and IP address with platform-specific optimizations.
Description
HostIdentifier is a utility class in the org.openqa.selenium.net package that resolves and caches the local machine's hostname and host address. It uses a multi-strategy approach: first checking environment variables (HOSTNAME on most OSes, COMPUTERNAME on Windows), then executing the hostname command on macOS (to avoid slow reverse DNS lookups), and finally falling back to InetAddress.getLocalHost(). For the host address, it checks the macOS en0 interface first, then falls back to InetAddress.getLocalHost().getHostAddress(). All results are cached using volatile fields and synchronized access. Failures are logged at WARNING level and fall back to "Unknown".
Usage
Use HostIdentifier when you need a quick, static way to obtain the local hostname or IP address without instantiating an object. This is suitable for logging, identification in distributed systems like Selenium Grid, or embedding host information in status messages.
Code Reference
Source Location
- Repository: SeleniumHQ_Selenium
- File: java/src/org/openqa/selenium/net/HostIdentifier.java
Signature
public class HostIdentifier {
public static synchronized String getHostName()
public static synchronized String getHostAddress()
}
Import
import org.openqa.selenium.net.HostIdentifier;
I/O Contract
| Method | Input | Output | Description |
|---|---|---|---|
getHostName() |
none | String -- hostname of the local machine |
Resolves hostname using environment variables (HOSTNAME, COMPUTERNAME), the hostname command on macOS, or InetAddress.getLocalHost().getHostName(). Returns "Unknown" if all methods fail. Result is cached after first call.
|
getHostAddress() |
none | String -- IP address of the local machine |
On macOS, checks en0 network interface first. Falls back to InetAddress.getLocalHost().getHostAddress(). Returns "Unknown" if all methods fail. Result is cached after first call.
|
Usage Examples
// Get the hostname (static, cached)
String hostname = HostIdentifier.getHostName();
// Get the host IP address (static, cached)
String address = HostIdentifier.getHostAddress();
// Use in logging or status messages
System.out.println("Running on: " + HostIdentifier.getHostName()
+ " (" + HostIdentifier.getHostAddress() + ")");
Related Pages
- Implementation:SeleniumHQ_Selenium_NetworkUtils -- More comprehensive network utility with instance methods and non-loopback address resolution
- Implementation:SeleniumHQ_Selenium_PortProber -- Free port detection utility in the same package
- Implementation:SeleniumHQ_Selenium_UrlChecker -- URL availability polling utility in the same package