public class OSUtil extends Object
Helps identifying the operating system on which the current application is running with
getOsName(), isWindows(), isLinux()m isMacOs() and
isAndroid().
Also enables finding the fully qualified domain name (FQDN) of the computer using various
techniques. The getHostNameWithDomain() and getHostNameWithoutDomain() try to
provide reliable results in the various operating systems using hostname() and
getDomainNameFromOs() and diverting to getLocalHostName() if they fail.
| Constructor and Description |
|---|
OSUtil() |
| Modifier and Type | Method and Description |
|---|---|
static String |
exec(String command)
Execute a command in the operating system using Runtime.exec and return the output as a string.
|
static String |
exec(String command,
int timeout,
TimeUnit timeoutUnit)
Execute a command in the operating system using Runtime.exec and return the output as a string.
|
static int |
getDefaultExecTimeout() |
static String |
getDomainNameFromOs()
Finds the DomainName from the operating system.
|
protected static List<String> |
getHostNamesFromNetworkInterfaces(boolean includeIpAddresses,
boolean includeIpv6)
Enumerates the NetworkInterfaces and finds the HostNames defined for those by InetAddress.
|
static String |
getHostNameWithDomain()
The HostName of the computer in which the application is run, with the Domain part, i.e.
|
static String |
getHostNameWithoutDomain()
The HostName of the computer in which the application is run, without the Domain part.
|
static String |
getLocalCanonicalHostName()
CanonicalHostName from InetAddress.
|
static String |
getLocalHostName()
HostName from
InetAddress. |
static String |
getOsName()
The current Operating System.
|
static boolean |
isAndroid() |
static boolean |
isLinux() |
static boolean |
isMacOs() |
static boolean |
isWindows() |
static void |
setDefaultExecTimeout(int defaultExecTimeout)
Sets the default timeout for commands in seconds.
|
public static String exec(String command) throws IOException
getDefaultExecTimeout().command - the command to runSecurityException - - If a security manager exists and its checkExec method doesn't allow
creation of the subprocessIOException - - If an I/O error occursCancellationException - - If the command doesn't return earlier than timeout or is
interruptedNullPointerException - - If command is nullpublic static String exec(String command, int timeout, TimeUnit timeoutUnit) throws IOException
command - the command to runtimeout - the timeout for the commandtimeoutUnit - unit for timeoutSecurityException - - If a security manager exists and its checkExec method doesn't allow
creation of the subprocessIOException - - If an I/O error occursCancellationException - - If the command doesn't return earlier than timeout or is
interruptedNullPointerException - - If command is nullpublic static int getDefaultExecTimeout()
public static String getDomainNameFromOs() throws IOException
IOException - if the OS command execution failspublic static String getHostNameWithDomain()
hostname(), except in Windows the Domain part is retrieved with
getDomainNameFromOs() and added to the hostname(). If
hostname() fails, returns getLocalHostName() instead.public static String getHostNameWithoutDomain() throws IOException
hostname() without the domain part in any operating system.IOException - if hostName call fails respectively.public static String getLocalCanonicalHostName()
public static String getLocalHostName()
InetAddress.InetAddress.getLocalHost()public static String getOsName()
public static boolean isAndroid()
public static boolean isLinux()
public static boolean isMacOs()
public static boolean isWindows()
public static void setDefaultExecTimeout(int defaultExecTimeout)
protected static List<String> getHostNamesFromNetworkInterfaces(boolean includeIpAddresses, boolean includeIpv6)
includeIpAddresses - if true, also adds pure IP addresses in the list. Otherwise, only
returns proper HostNames.includeIpv6 - if true, also searches IPV6 addresses. Otherwise, returns only addresses for
IPV4.Copyright © 2026. All rights reserved.