Interface OS


public interface OS
Operating system utility functions.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The name of the operating system.
  • Method Summary

    Static Methods
    Modifier and Type
    Method
    Description
    static String
    exec(String command, int timeout)
    Runs a bash command with timeout.
    static Process
    exec(String command, Path file)
    Runs a bash command and redirects output to a file.
    static Process
    exec(String command, Consumer<String> outputConsumer)
    Executes a shell command in a separate process.
    static Process
    exec(List<String> command, Path file)
    Executes a system command in a separate process.
    static Process
    exec(List<String> command, Consumer<String> outputConsumer)
    Executes a system command in a separate process.
    static double
    getProperty(String key, double defaultValue)
    Returns the value of the system property as a double, or the default value if the property is not set or invalid.
    static float
    getProperty(String key, float defaultValue)
    Returns the value of the system property as a float, or the default value if the property is not set or invalid.
    static int
    getProperty(String key, int defaultValue)
    Returns the value of the system property as an integer, or the default value if the property is not set or invalid.
    static boolean
    Returns true if the operating system is macOS.
    static boolean
    Returns true if the operating system is Linux or Unix.
    static boolean
    Returns true if the operating system is Windows.
    static List<String>
    parse(String command)
    Parses a command line into a list of arguments, respecting quoted substrings.
    static List<String>
    shell(String command)
    Returns the shell command with parsed arguments, respecting quoted substrings.
  • Field Details

    • name

      static final String name
      The name of the operating system.
  • Method Details

    • isWindows

      static boolean isWindows()
      Returns true if the operating system is Windows.
      Returns:
      true if the operating system is Windows.
    • isMacOS

      static boolean isMacOS()
      Returns true if the operating system is macOS.
      Returns:
      true if the operating system is macOS.
    • isUnix

      static boolean isUnix()
      Returns true if the operating system is Linux or Unix.
      Returns:
      true if the operating system is Linux or Unix.
    • getProperty

      static int getProperty(String key, int defaultValue)
      Returns the value of the system property as an integer, or the default value if the property is not set or invalid.
      Parameters:
      key - the name of the system property.
      defaultValue - the default value to return if the property is not set or invalid.
      Returns:
      the integer value of the system property, or the default value if not set or invalid.
    • getProperty

      static float getProperty(String key, float defaultValue)
      Returns the value of the system property as a float, or the default value if the property is not set or invalid.
      Parameters:
      key - the name of the system property.
      defaultValue - the default value to return if the property is not set or invalid.
      Returns:
      the float value of the system property, or the default value if not set or invalid.
    • getProperty

      static double getProperty(String key, double defaultValue)
      Returns the value of the system property as a double, or the default value if the property is not set or invalid.
      Parameters:
      key - the name of the system property.
      defaultValue - the default value to return if the property is not set or invalid.
      Returns:
      the double value of the system property, or the default value if not set or invalid.
    • parse

      static List<String> parse(String command)
      Parses a command line into a list of arguments, respecting quoted substrings.
      Parameters:
      command - the command line to parse.
      Returns:
      the list of command arguments.
    • shell

      static List<String> shell(String command)
      Returns the shell command with parsed arguments, respecting quoted substrings.
      Parameters:
      command - the command line to parse.
      Returns:
      the list of command arguments.
    • exec

      static Process exec(String command, Path file) throws IOException
      Runs a bash command and redirects output to a file.
      Parameters:
      command - the command line to run.
      file - the output file.
      Returns:
      the output of the command, or error message if failed.
      Throws:
      IOException - when fails to execute the command.
    • exec

      static Process exec(List<String> command, Path file) throws IOException
      Executes a system command in a separate process.
      Parameters:
      command - the program and its arguments.
      file - the file to redirect output to.
      Returns:
      the process.
      Throws:
      IOException - when fails to execute the command.
    • exec

      static Process exec(List<String> command, Consumer<String> outputConsumer) throws IOException
      Executes a system command in a separate process.
      Parameters:
      command - the program and its arguments.
      outputConsumer - the consumer to handle the output lines from the command.
      Returns:
      the process.
      Throws:
      IOException - when fails to execute the command.
    • exec

      static Process exec(String command, Consumer<String> outputConsumer) throws IOException
      Executes a shell command in a separate process.
      Parameters:
      command - the command line to run.
      outputConsumer - the consumer to handle the output lines from the command.
      Returns:
      the process.
      Throws:
      IOException - when fails to execute the command.
    • exec

      static String exec(String command, int timeout)
      Runs a bash command with timeout.
      Parameters:
      command - the command line to run.
      timeout - the timeout in milliseconds.
      Returns:
      the output of the command, or error message if failed.