Class SysEnv


  • public class SysEnv
    extends java.lang.Object
    A class to provide access to OS environment variables, by means of an external command which is executed.

    The command will be executed when the first access is made to the environment variables. The command name is determined as follows:

    • The value passed to setCommand, if that has been set
    • The value of the system property "javatest.sysEnv.command", if that has been set
    • An OS-specific default, if one is known. The current set of defaults is as follows:
      The current set of defaults
      OS Default
      Mac OS X /usr/bin/env
      Solaris /usr/bin/env
      Windows XP cmd /c set
    • Finally, a simple default of "env" is used. While this may not always work by default, a user could provide a suitable script or batch file on the current execution path that will yield the required results.

    Note that the specified command will be invoked by Runtime.exec and must be capable or working as such. This may preclude direct use of builtin commands on some systems. For example, you cannot directly exec the Windows "set" command built in to the standard Windows command shell.

    The command must print out a series of lines of the form name=value, one for each environment variable.

    • Constructor Summary

      Constructors 
      Constructor Description
      SysEnv()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String get​(java.lang.String name)
      Get the value of a specified environment value.
      static java.util.Map<java.lang.String,​java.lang.String> getAll()
      Get a map containing all of the environment variables in the current execution context.
      static java.util.Map<java.lang.String,​java.lang.String> getAll​(java.util.Map<java.lang.String,​java.lang.String> m)
      Get a map containing all of the environment variables in the current execution context.
      static void setCommand​(java.lang.String cmd)
      Set the command to be executed to access the OS environment variables.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SysEnv

        public SysEnv()
    • Method Detail

      • setCommand

        public static void setCommand​(java.lang.String cmd)
        Set the command to be executed to access the OS environment variables. To be effective, this method must be set before any of the get methods,
        Parameters:
        cmd - the command to be executed
      • get

        public static java.lang.String get​(java.lang.String name)
        Get the value of a specified environment value.
        Parameters:
        name - the name of the environment variable
        Returns:
        the value of the environment variable if set, or null if not
      • getAll

        public static java.util.Map<java.lang.String,​java.lang.String> getAll()
        Get a map containing all of the environment variables in the current execution context.
        Returns:
        a map containing all the known environment variables.
      • getAll

        public static java.util.Map<java.lang.String,​java.lang.String> getAll​(java.util.Map<java.lang.String,​java.lang.String> m)
        Get a map containing all of the environment variables in the current execution context.
        Parameters:
        m - the map in which to put the names and values of all the environment variables in the current execution context.
        Returns:
        the argument map.