Class OS

java.lang.Object
org.daiitech.naftah.utils.OS

public final class OS extends Object
Utility class for detecting the current operating system's properties, including name, architecture, version, and system family.

Provides a set of static methods for querying the current environment, particularly useful for platform-specific behavior.

This class is not instantiable.

Examples:


 if (OS.isFamilyWindows()) {
     // Windows-specific logic
 }
 

Internally uses System.getProperty and lowercase matching.

Author:
Chakib Daii
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final String
    Identifier for DOS family.
    private static final String
    Identifier for Mac family.
    private static final String
    Identifier for NetWare family.
    private static final String
    Identifier for OpenVMS family.
    private static final String
    Identifier for OS/2 family.
    private static final String
    Identifier for OS/400 family.
    private static final String
    Identifier for Tandem family.
    private static final String
    Identifier for Unix family.
    private static final String
    Identifier for Windows 9x family (legacy Windows).
    private static final String
    Identifier for Windows family.
    private static final String
    Identifier for z/OS family.
    private static final boolean
    Whether the current OS is running under Windows Subsystem for Linux (WSL).
    private static final boolean
    Whether the current terminal is a real XTerm instance.
    private static final String
    The OS architecture in lowercase, retrieved from system properties.
    static final String
    System property key for the operating system architecture.
    private static final String
    The OS name in lowercase, retrieved from system properties.
    static final String
    System property key for the operating system name.
    private static final String
    The OS version in lowercase, retrieved from system properties.
    static final String
    System property key for the operating system version.
    private static final String
    The system path separator character(s).
    static final String
    Terminal type identifier provided via terminfo (often misleading).
    static final String
    WSL environment variable containing the current Linux distribution name.
    static final String
    WSL-specific environment variable controlling Windows–Linux env propagation.
    static final String
    WSL environment variable used for Windows–Linux process interop.
    static final String
    Set only by real xterm to expose its version and identify the emulator.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    OS()
    Private constructor to prevent instantiation.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static boolean
    Detects whether the JVM is running inside a real XTerm terminal.
    private static boolean
    Detects whether the JVM is running inside Windows Subsystem for Linux (WSL).
    static boolean
    isArch(String arch)
    Checks whether the current OS architecture matches the given arch exactly.
    private static boolean
    isFamily(String family)
    Checks if the current OS matches the specified family.
    static boolean
    Checks if the current OS belongs to the DOS family.
    static boolean
    Checks if the current OS belongs to the Mac family.
    static boolean
    Checks if the current OS belongs to the NetWare family.
    static boolean
    Checks if the current OS belongs to the OpenVMS family.
    static boolean
    Checks if the current OS belongs to the OS/2 family.
    static boolean
    Checks if the current OS belongs to the OS/400 family.
    static boolean
    Checks if the current OS belongs to the Tandem family.
    static boolean
    Checks if the current OS belongs to the Unix family.
    static boolean
    Checks if the current OS belongs to the legacy Windows 9x family.
    static boolean
    Checks if the current OS belongs to the Windows family.
    static boolean
    Checks if the current OS belongs to the z/OS family.
    static boolean
    isName(String name)
    Checks whether the current OS name matches the given name exactly.
    static boolean
    isOs(String family, String name, String arch, String version)
    Checks whether the current OS matches a combination of criteria.
    static boolean
    Checks whether the current terminal is a real XTerm instance.
    static boolean
    isVersion(String version)
    Checks whether the current OS version matches the given version exactly.
    static boolean
    Checks whether the current OS is running under Windows Subsystem for Linux (WSL).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • OS_NAME_PROPERTY

      public static final String OS_NAME_PROPERTY
      System property key for the operating system name.
      See Also:
    • OS_ARCH_PROPERTY

      public static final String OS_ARCH_PROPERTY
      System property key for the operating system architecture.
      See Also:
    • OS_VERSION_PROPERTY

      public static final String OS_VERSION_PROPERTY
      System property key for the operating system version.
      See Also:
    • WSL_DISTRO_NAME_ENV

      public static final String WSL_DISTRO_NAME_ENV
      WSL environment variable containing the current Linux distribution name.
      See Also:
    • WSL_INTEROP_ENV

      public static final String WSL_INTEROP_ENV
      WSL environment variable used for Windows–Linux process interop.
      See Also:
    • WSL_ENV

      public static final String WSL_ENV
      WSL-specific environment variable controlling Windows–Linux env propagation.
      See Also:
    • TERM_ENV

      public static final String TERM_ENV
      Terminal type identifier provided via terminfo (often misleading).
      See Also:
    • XTERM_VERSION_ENV

      public static final String XTERM_VERSION_ENV
      Set only by real xterm to expose its version and identify the emulator.
      See Also:
    • FAMILY_OS_400

      private static final String FAMILY_OS_400
      Identifier for OS/400 family.
      See Also:
    • FAMILY_Z_OS

      private static final String FAMILY_Z_OS
      Identifier for z/OS family.
      See Also:
    • FAMILY_WIN9X

      private static final String FAMILY_WIN9X
      Identifier for Windows 9x family (legacy Windows).
      See Also:
    • FAMILY_OPENVMS

      private static final String FAMILY_OPENVMS
      Identifier for OpenVMS family.
      See Also:
    • FAMILY_UNIX

      private static final String FAMILY_UNIX
      Identifier for Unix family.
      See Also:
    • FAMILY_TANDEM

      private static final String FAMILY_TANDEM
      Identifier for Tandem family.
      See Also:
    • FAMILY_MAC

      private static final String FAMILY_MAC
      Identifier for Mac family.
      See Also:
    • FAMILY_DOS

      private static final String FAMILY_DOS
      Identifier for DOS family.
      See Also:
    • FAMILY_NETWARE

      private static final String FAMILY_NETWARE
      Identifier for NetWare family.
      See Also:
    • FAMILY_OS_2

      private static final String FAMILY_OS_2
      Identifier for OS/2 family.
      See Also:
    • FAMILY_WINDOWS

      private static final String FAMILY_WINDOWS
      Identifier for Windows family.
      See Also:
    • IS_WSL

      private static final boolean IS_WSL
      Whether the current OS is running under Windows Subsystem for Linux (WSL).
    • IS_XTERM

      private static final boolean IS_XTERM
      Whether the current terminal is a real XTerm instance.
    • OS_NAME

      private static final String OS_NAME
      The OS name in lowercase, retrieved from system properties.
    • OS_ARCH

      private static final String OS_ARCH
      The OS architecture in lowercase, retrieved from system properties.
    • OS_VERSION

      private static final String OS_VERSION
      The OS version in lowercase, retrieved from system properties.
    • PATH_SEP

      private static final String PATH_SEP
      The system path separator character(s).
  • Constructor Details

    • OS

      private OS()
      Private constructor to prevent instantiation. Always throws a NaftahBugError when called.
  • Method Details

    • checkIfInsideWSL

      private static boolean checkIfInsideWSL()
      Detects whether the JVM is running inside Windows Subsystem for Linux (WSL).

      Checks environment variables first, then falls back to inspecting the kernel version in /proc/version for "Microsoft".

      Returns:
      true if running inside WSL, false otherwise
    • checkIfInsideRealXTerm

      private static boolean checkIfInsideRealXTerm()
      Detects whether the JVM is running inside a real XTerm terminal.

      Relies on TERM and XTERM_VERSION environment variables to distinguish XTerm from other terminal emulators (like GNOME Terminal or Kitty).

      Returns:
      true if running inside XTerm, false otherwise
    • isFamily

      private static boolean isFamily(String family)
      Checks if the current OS matches the specified family.
      Parameters:
      family - the OS family name to check
      Returns:
      true if current OS belongs to that family
    • isFamilyDOS

      public static boolean isFamilyDOS()
      Checks if the current OS belongs to the DOS family.
      Returns:
      true if the OS is in the DOS family, false otherwise.
    • isFamilyMac

      public static boolean isFamilyMac()
      Checks if the current OS belongs to the Mac family.
      Returns:
      true if the OS is in the Mac family, false otherwise.
    • isFamilyNetware

      public static boolean isFamilyNetware()
      Checks if the current OS belongs to the NetWare family.
      Returns:
      true if the OS is in the NetWare family, false otherwise.
    • isFamilyOS2

      public static boolean isFamilyOS2()
      Checks if the current OS belongs to the OS/2 family.
      Returns:
      true if the OS is in the OS/2 family, false otherwise.
    • isFamilyTandem

      public static boolean isFamilyTandem()
      Checks if the current OS belongs to the Tandem family.
      Returns:
      true if the OS is in the Tandem family, false otherwise.
    • isFamilyUnix

      public static boolean isFamilyUnix()
      Checks if the current OS belongs to the Unix family.
      Returns:
      true if the OS is in the Unix family, false otherwise.
    • isFamilyWindows

      public static boolean isFamilyWindows()
      Checks if the current OS belongs to the Windows family.
      Returns:
      true if the OS is in the Windows family, false otherwise.
    • isWSL

      public static boolean isWSL()
      Checks whether the current OS is running under Windows Subsystem for Linux (WSL).
      Returns:
      true if running in WSL, false otherwise
    • isRealXTerm

      public static boolean isRealXTerm()
      Checks whether the current terminal is a real XTerm instance.
      Returns:
      true if running inside XTerm, false otherwise
    • isFamilyWin9x

      public static boolean isFamilyWin9x()
      Checks if the current OS belongs to the legacy Windows 9x family.
      Returns:
      true if the OS is in the Windows 9x family, false otherwise.
    • isFamilyZOS

      public static boolean isFamilyZOS()
      Checks if the current OS belongs to the z/OS family.
      Returns:
      true if the OS is in the z/OS family, false otherwise.
    • isFamilyOS400

      public static boolean isFamilyOS400()
      Checks if the current OS belongs to the OS/400 family.
      Returns:
      true if the OS is in the OS/400 family, false otherwise.
    • isFamilyOpenVms

      public static boolean isFamilyOpenVms()
      Checks if the current OS belongs to the OpenVMS family.
      Returns:
      true if the OS is in the OpenVMS family, false otherwise.
    • isName

      public static boolean isName(String name)
      Checks whether the current OS name matches the given name exactly.
      Parameters:
      name - the OS name to check
      Returns:
      true if matched
    • isArch

      public static boolean isArch(String arch)
      Checks whether the current OS architecture matches the given arch exactly.
      Parameters:
      arch - the architecture string to match
      Returns:
      true if matched
    • isVersion

      public static boolean isVersion(String version)
      Checks whether the current OS version matches the given version exactly.
      Parameters:
      version - the OS version to check
      Returns:
      true if matched
    • isOs

      public static boolean isOs(String family, String name, String arch, String version)
      Checks whether the current OS matches a combination of criteria. Any null parameter is ignored in the match.
      Parameters:
      family - the OS family name (e.g., "windows")
      name - the OS name (e.g., "Windows 10")
      arch - the architecture (e.g., "x86_64")
      version - the version (e.g., "10.0")
      Returns:
      true if all specified parameters match the current system
      Throws:
      NaftahBugError - if an unknown family is passed