Interface SystemProperties

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface SystemProperties
A source of system properties: the running JVM's, or fixed ones for tests.

Code that reads system properties through a SystemProperties it is given, rather than through System.getProperty(String), can be tested with of(Map).

Besides get(String), which reads any property, there is a typed method for each property listed in the Java SE 25 documentation of System.getProperties(), followed by the ones the JDK supports. The properties every Java runtime must define are returned as is, and throw NoSuchElementException if the source lacks them, which only a fixed source can. The others are returned as an Optional, or as an empty list for paths.

Paths are returned in the default file system, as Path.of(String, String...) makes them. Path lists, such as javaClassPath(), are split on the path separator of the same source, with empty entries skipped. A Platform may be more appropriate for paths: its directories are in the file system it was given, which tests can replace. get(String) still gives any value as a string.

  • Method Summary

    Modifier and Type
    Method
    Description
    default Optional<String>
    Returns the name of the default charset.
    default String
    Returns the file separator, "/" on Unix: the file.separator property.
    get(String name)
    Returns the value of a property.
    default List<Path>
    Returns the class path, split on the path separator: the java.class.path property.
    default String
    Returns the latest class file format version the Java runtime recognizes, as MAJOR.MINOR: the java.class.version property.
    default Path
    Returns the installation directory of Java: the java.home property.
    default Path
    Returns the default temporary file directory: the java.io.tmpdir property.
    default List<Path>
    Returns the paths searched when loading libraries, split on the path separator: the java.library.path property.
    default OptionalInt
    Returns the maintenance version of the Java Runtime Environment specification, which is set only after a maintenance release: the java.specification.maintenance.version property.
    default String
    Returns the name of the Java Runtime Environment specification: the java.specification.name property.
    default String
    Returns the vendor of the Java Runtime Environment specification: the java.specification.vendor property.
    default int
    Returns the version of the Java Runtime Environment specification: the feature element of the runtime version, such as 21: the java.specification.version property.
    default String
    Returns the vendor of the Java Runtime Environment: the java.vendor property.
    default URI
    Returns the URL of the Java vendor: the java.vendor.url property.
    default Optional<String>
    Returns the version of the Java vendor, which is optional: the java.vendor.version property.
    Returns the version of the Java Runtime Environment: the java.version property.
    default LocalDate
    Returns the version date of the Java Runtime Environment: the java.version.date property.
    default String
    Returns the name of the Java Virtual Machine implementation: the java.vm.name property.
    default String
    Returns the name of the Java Virtual Machine specification: the java.vm.specification.name property.
    default String
    Returns the vendor of the Java Virtual Machine specification: the java.vm.specification.vendor property.
    default int
    Returns the version of the Java Virtual Machine specification: the feature element of the runtime version, such as 21: the java.vm.specification.version property.
    default String
    Returns the vendor of the Java Virtual Machine implementation: the java.vm.vendor property.
    Returns the version of the Java Virtual Machine implementation: the java.vm.version property.
    default Optional<String>
    Returns the name of the initial (main) module, if the application was launched from a module: the jdk.module.main property.
    default Optional<String>
    Returns the main class of the initial module, if the application was launched from a module: the jdk.module.main.class property.
    default List<Path>
    Returns the application module path, split on the path separator; empty when the application does not use one: the jdk.module.path property.
    default List<Path>
    Returns the upgrade module path, split on the path separator; empty when there is none: the jdk.module.upgrade.path property.
    default String
    Returns the line separator, "\n" on Unix: the line.separator property.
    default Charset
    Returns the character encoding derived from the host environment and the user's settings: the native.encoding property.
    of(Map<String,String> properties)
    Returns fixed system properties.
    default String
    Returns the architecture of the operating system: the os.arch property.
    default String
    Returns the name of the operating system: the os.name property.
    default String
    Returns the version of the operating system: the os.version property.
    default String
    Returns the path separator, ":" on Unix: the path.separator property.
    default Charset
    Returns the character encoding of System.err: the stderr.encoding property.
    default Charset
    Returns the character encoding of System.in: the stdin.encoding property.
    default Charset
    Returns the character encoding of System.out: the stdout.encoding property.
    Returns the system properties of the running JVM.
    default Path
    Returns the current working directory of the user: the user.dir property.
    default Path
    Returns the home directory of the user: the user.home property.
    default String
    Returns the name of the user account: the user.name property.
  • Method Details

    • system

      static SystemProperties system()
      Returns the system properties of the running JVM. Each call reads the current value.
      Returns:
      the JVM's system properties
    • of

      static SystemProperties of(Map<String,String> properties)
      Returns fixed system properties.
      Parameters:
      properties - the properties, by name; copied
      Returns:
      system properties holding exactly properties
    • get

      Optional<String> get(String name)
      Returns the value of a property.
      Parameters:
      name - the name of the property, such as "user.home"
      Returns:
      the value, or empty if the property is not set
      Throws:
      IllegalArgumentException - if name is empty
    • javaVersion

      default Runtime.Version javaVersion()
      Returns the version of the Java Runtime Environment: the java.version property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
      IllegalArgumentException - if the property is not a valid version
    • javaVersionDate

      default LocalDate javaVersionDate()
      Returns the version date of the Java Runtime Environment: the java.version.date property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
      DateTimeParseException - if the property is not a valid date
    • javaVendor

      default String javaVendor()
      Returns the vendor of the Java Runtime Environment: the java.vendor property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
    • javaVendorUrl

      default URI javaVendorUrl()
      Returns the URL of the Java vendor: the java.vendor.url property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
      IllegalArgumentException - if the property is not a valid URI
    • javaVendorVersion

      default Optional<String> javaVendorVersion()
      Returns the version of the Java vendor, which is optional: the java.vendor.version property.
      Returns:
      the value, or empty if it is not set
    • javaHome

      default Path javaHome()
      Returns the installation directory of Java: the java.home property.

      The path is in the default file system, as Path.of(String, String...) makes it. A Platform may be more appropriate: its paths are in the file system it was given, which tests can replace.

      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
      InvalidPathException - if the value is not a path of the default file system
    • javaVmSpecificationVersion

      default int javaVmSpecificationVersion()
      Returns the version of the Java Virtual Machine specification: the feature element of the runtime version, such as 21: the java.vm.specification.version property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
      NumberFormatException - if the property is not an integer
    • javaVmSpecificationVendor

      default String javaVmSpecificationVendor()
      Returns the vendor of the Java Virtual Machine specification: the java.vm.specification.vendor property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
    • javaVmSpecificationName

      default String javaVmSpecificationName()
      Returns the name of the Java Virtual Machine specification: the java.vm.specification.name property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
    • javaVmVersion

      default Runtime.Version javaVmVersion()
      Returns the version of the Java Virtual Machine implementation: the java.vm.version property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
      IllegalArgumentException - if the property is not a valid version
    • javaVmVendor

      default String javaVmVendor()
      Returns the vendor of the Java Virtual Machine implementation: the java.vm.vendor property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
    • javaVmName

      default String javaVmName()
      Returns the name of the Java Virtual Machine implementation: the java.vm.name property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
    • javaSpecificationVersion

      default int javaSpecificationVersion()
      Returns the version of the Java Runtime Environment specification: the feature element of the runtime version, such as 21: the java.specification.version property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
      NumberFormatException - if the property is not an integer
    • javaSpecificationMaintenanceVersion

      default OptionalInt javaSpecificationMaintenanceVersion()
      Returns the maintenance version of the Java Runtime Environment specification, which is set only after a maintenance release: the java.specification.maintenance.version property.
      Returns:
      the value, or empty if it is not set
      Throws:
      NumberFormatException - if the property is set but is not an integer
    • javaSpecificationVendor

      default String javaSpecificationVendor()
      Returns the vendor of the Java Runtime Environment specification: the java.specification.vendor property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
    • javaSpecificationName

      default String javaSpecificationName()
      Returns the name of the Java Runtime Environment specification: the java.specification.name property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
    • javaClassVersion

      default String javaClassVersion()
      Returns the latest class file format version the Java runtime recognizes, as MAJOR.MINOR: the java.class.version property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
    • javaClassPath

      default List<Path> javaClassPath()
      Returns the class path, split on the path separator: the java.class.path property.

      The paths are in the default file system, as Path.of(String, String...) makes them. A Platform may be more appropriate: its paths are in the file system it was given, which tests can replace.

      Returns:
      the value
      Throws:
      NoSuchElementException - if the property or the path.separator property is not set
      IllegalArgumentException - if the path.separator property is empty
      InvalidPathException - if an entry is not a path of the default file system
    • javaLibraryPath

      default List<Path> javaLibraryPath()
      Returns the paths searched when loading libraries, split on the path separator: the java.library.path property.

      The paths are in the default file system, as Path.of(String, String...) makes them. A Platform may be more appropriate: its paths are in the file system it was given, which tests can replace.

      Returns:
      the value
      Throws:
      NoSuchElementException - if the property or the path.separator property is not set
      IllegalArgumentException - if the path.separator property is empty
      InvalidPathException - if an entry is not a path of the default file system
    • javaIoTmpdir

      default Path javaIoTmpdir()
      Returns the default temporary file directory: the java.io.tmpdir property.

      The path is in the default file system, as Path.of(String, String...) makes it. A Platform may be more appropriate: its paths are in the file system it was given, which tests can replace.

      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
      InvalidPathException - if the value is not a path of the default file system
    • osName

      default String osName()
      Returns the name of the operating system: the os.name property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
    • osArch

      default String osArch()
      Returns the architecture of the operating system: the os.arch property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
    • osVersion

      default String osVersion()
      Returns the version of the operating system: the os.version property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
    • fileSeparator

      default String fileSeparator()
      Returns the file separator, "/" on Unix: the file.separator property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
    • pathSeparator

      default String pathSeparator()
      Returns the path separator, ":" on Unix: the path.separator property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
    • lineSeparator

      default String lineSeparator()
      Returns the line separator, "\n" on Unix: the line.separator property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
    • userName

      default String userName()
      Returns the name of the user account: the user.name property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
    • userHome

      default Path userHome()
      Returns the home directory of the user: the user.home property.

      The path is in the default file system, as Path.of(String, String...) makes it. A Platform may be more appropriate: the home of its directories is in the file system it was given, which tests can replace.

      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
      InvalidPathException - if the value is not a path of the default file system
    • userDir

      default Path userDir()
      Returns the current working directory of the user: the user.dir property.

      The path is in the default file system, as Path.of(String, String...) makes it. A Platform may be more appropriate: its paths are in the file system it was given, which tests can replace.

      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
      InvalidPathException - if the value is not a path of the default file system
    • nativeEncoding

      default Charset nativeEncoding()
      Returns the character encoding derived from the host environment and the user's settings: the native.encoding property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
      IllegalCharsetNameException - if the property is not a legal charset name
      UnsupportedCharsetException - if this JVM does not support the charset the property names
    • stdinEncoding

      default Charset stdinEncoding()
      Returns the character encoding of System.in: the stdin.encoding property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
      IllegalCharsetNameException - if the property is not a legal charset name
      UnsupportedCharsetException - if this JVM does not support the charset the property names
      Since:
      0.2.0
    • stdoutEncoding

      default Charset stdoutEncoding()
      Returns the character encoding of System.out: the stdout.encoding property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
      IllegalCharsetNameException - if the property is not a legal charset name
      UnsupportedCharsetException - if this JVM does not support the charset the property names
    • stderrEncoding

      default Charset stderrEncoding()
      Returns the character encoding of System.err: the stderr.encoding property.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is not set
      IllegalCharsetNameException - if the property is not a legal charset name
      UnsupportedCharsetException - if this JVM does not support the charset the property names
    • jdkModulePath

      default List<Path> jdkModulePath()
      Returns the application module path, split on the path separator; empty when the application does not use one: the jdk.module.path property.

      Supported by the JDK, not required by the Java SE specification.

      The paths are in the default file system, as Path.of(String, String...) makes them. A Platform may be more appropriate: its paths are in the file system it was given, which tests can replace.

      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is set but the path.separator property is not
      IllegalArgumentException - if the property is set but the path.separator property is empty
      InvalidPathException - if an entry is not a path of the default file system
    • jdkModuleUpgradePath

      default List<Path> jdkModuleUpgradePath()
      Returns the upgrade module path, split on the path separator; empty when there is none: the jdk.module.upgrade.path property.

      Supported by the JDK, not required by the Java SE specification.

      The paths are in the default file system, as Path.of(String, String...) makes them. A Platform may be more appropriate: its paths are in the file system it was given, which tests can replace.

      Returns:
      the value
      Throws:
      NoSuchElementException - if the property is set but the path.separator property is not
      IllegalArgumentException - if the property is set but the path.separator property is empty
      InvalidPathException - if an entry is not a path of the default file system
    • jdkModuleMain

      default Optional<String> jdkModuleMain()
      Returns the name of the initial (main) module, if the application was launched from a module: the jdk.module.main property.

      Supported by the JDK, not required by the Java SE specification.

      Returns:
      the value, or empty if it is not set
    • jdkModuleMainClass

      default Optional<String> jdkModuleMainClass()
      Returns the main class of the initial module, if the application was launched from a module: the jdk.module.main.class property.

      Supported by the JDK, not required by the Java SE specification.

      Returns:
      the value, or empty if it is not set
    • fileEncoding

      default Optional<String> fileEncoding()
      Returns the name of the default charset. A name rather than a Charset: it can be set on the command line to COMPAT, which is not a charset name: the file.encoding property.

      Supported by the JDK, not required by the Java SE specification.

      Returns:
      the value, or empty if it is not set