Class WinEnvVars


  • public class WinEnvVars
    extends java.lang.Object
    Collection of static methods to get and modify environment variables on Windows.
    • Constructor Summary

      Constructors 
      Constructor Description
      WinEnvVars()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void appendToPath​(java.lang.String value)
      Appends one or more directories to the PATH environment variable.
      static void appendToUserPath​(java.lang.String value)
      Appends one or more directories to the user-specific PATH environment variable.
      static java.util.Properties getenv()
      Returns all environment variables.
      static java.lang.String getenv​(java.lang.String key)
      Returns a single environment variable.
      static java.lang.String getFromRegistry​(java.lang.String key, boolean userSpecific)
      Return an environment variable as set in the Windows registry.
      static void prependToPath​(java.lang.String value)
      Prepends one or more directories to the PATH environment variable.
      static void prependToUserPath​(java.lang.String value)
      Prepends one or more directories to the user-specific PATH environment variable.
      static void set​(java.lang.String key, java.lang.String value)
      Sets a global environment variable.
      static void setUserSpecific​(java.lang.String key, java.lang.String value)
      Sets a user-specific environment variable.
      • Methods inherited from class java.lang.Object

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

      • WinEnvVars

        public WinEnvVars()
    • Method Detail

      • set

        public static void set​(java.lang.String key,
                               java.lang.String value)
                        throws java.io.IOException
        Sets a global environment variable. Needs Administrator rights.
        Parameters:
        key - the name of the environment variable
        value - the value of the environment variable
        Throws:
        java.io.IOException - if the environment variable could not be set
      • setUserSpecific

        public static void setUserSpecific​(java.lang.String key,
                                           java.lang.String value)
                                    throws java.io.IOException
        Sets a user-specific environment variable.
        Parameters:
        key - the name of the environment variable
        value - the value of the environment variable
        Throws:
        java.io.IOException - if the environment variable could not be set
      • getenv

        public static java.lang.String getenv​(java.lang.String key)
        Returns a single environment variable.
        Parameters:
        key - the name of the environment variable. Case does not matter.
        Returns:
        the value of the environment variable or null if not defined
      • getenv

        public static java.util.Properties getenv()
        Returns all environment variables.
        Returns:
        a property map with all environment variables and its values
      • getFromRegistry

        public static java.lang.String getFromRegistry​(java.lang.String key,
                                                       boolean userSpecific)
                                                throws java.io.IOException
        Return an environment variable as set in the Windows registry. The user-specific setting may be different from the value returned in getenv(). For example, the PATH environment variable is modified by the install4j launcher and is not equal to the system setting. In addition, the value returned for PATH will have no variable substitutions performed.
        Parameters:
        userSpecific - If true the user-specific variant should be retrieved. Otherwise the global setting will be returned.
        Returns:
        the value of the environment variable or null if not defined
        Throws:
        java.io.IOException - if the environment variable could not be retrieved
      • appendToPath

        public static void appendToPath​(java.lang.String value)
                                 throws java.io.IOException
        Appends one or more directories to the PATH environment variable. Needs Administrator rights.
        Parameters:
        value - the value to be appended to the path
        Throws:
        java.io.IOException - if the environment variable could not be set
      • prependToPath

        public static void prependToPath​(java.lang.String value)
                                  throws java.io.IOException
        Prepends one or more directories to the PATH environment variable. Needs Administrator rights.
        Parameters:
        value - the value to be appended to the path
        Throws:
        java.io.IOException - if the environment variable could not be set
      • appendToUserPath

        public static void appendToUserPath​(java.lang.String value)
                                     throws java.io.IOException
        Appends one or more directories to the user-specific PATH environment variable.
        Parameters:
        value - the value to be appended to the path
        Throws:
        java.io.IOException - if the environment variable could not be set
        See Also:
        appendToPath(String)
      • prependToUserPath

        public static void prependToUserPath​(java.lang.String value)
                                      throws java.io.IOException
        Prepends one or more directories to the user-specific PATH environment variable.
        Parameters:
        value - the value to be appended to the path
        Throws:
        java.io.IOException - if the environment variable could not be set
        See Also:
        prependToPath(String)