public final class EnvironmentUtil
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
EnvironmentUtil.ShellEnvReader |
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
BASH_EXECUTABLE_NAME |
static java.lang.String |
SHELL_COMMAND_ARGUMENT |
static java.lang.String |
SHELL_VARIABLE_NAME |
Modifier and Type | Method and Description |
---|---|
static java.util.List<java.lang.String> |
buildShellProcessCommand(java.lang.String shellScript,
boolean isLogin,
boolean isInteractive,
boolean isCommand)
Builds a login shell command list from the
shellScript path. |
static java.lang.String [] |
flattenEnvironment(java.util.Map<java.lang.String,java.lang.String> environment) |
static java.lang.String [] |
getEnvironment()
Same as
flattenEnvironment(getEnvironmentMap()) . |
static java.util.Map<java.lang.String,java.lang.String> |
getEnvironmentMap()
A wrapper layer around
System.getenv() . |
static java.lang.String |
getValue(java.lang.String name)
Same as
getEnvironmentMap().get(name) . |
static void |
inlineParentOccurrences(java.util.Map<java.lang.String,java.lang.String> envs) |
static void |
inlineParentOccurrences(java.util.Map<java.lang.String,java.lang.String> envs,
java.util.Map<java.lang.String,java.lang.String> parentEnv) |
static boolean |
isValidName(java.lang.String name)
Validates environment variable name in accordance to
ProcessEnvironment#validateVariable (ProcessEnvironment#validateName on Windows). |
static boolean |
isValidValue(java.lang.String value)
Validates environment variable value in accordance to
ProcessEnvironment#validateValue . |
static java.util.concurrent.CompletableFuture<java.util.Map<java.lang.String,java.lang.String>> |
loadEnv() |
static java.util.Map<java.lang.String,java.lang.String> |
parseEnv(java.lang.String... lines) |
static java.lang.String |
setLocaleEnv(java.util.Map<java.lang.String,java.lang.String> env,
java.nio.charset.Charset charset) |
public static final java.lang.String BASH_EXECUTABLE_NAME
public static final java.lang.String SHELL_VARIABLE_NAME
public static final java.lang.String SHELL_COMMAND_ARGUMENT
public static java.util.concurrent.CompletableFuture<java.util.Map<java.lang.String,java.lang.String>> loadEnv()
public static java.util.Map<java.lang.String,java.lang.String> getEnvironmentMap()
System.getenv()
.
On Windows, the returned map is case-insensitive (i.e. map.get("Path") == map.get("PATH")
holds).
On Mac OS X things are complicated.
An app launched by a GUI launcher (Finder, Dock, Spotlight etc.) receives a pretty empty and useless environment,
since standard Unix ways of setting variables via e.g. ~/.profile do not work. What's more important, there are no
sane alternatives. This causes a lot of user complaints about tools working in a terminal not working when launched
from the IDE. To ease their pain, the IDE loads a shell environment (see getShellEnv()
for gory details)
and returns it as the result.
And one more thing (c): locale variables on OS X are usually set by a terminal app - meaning they are missing
even from a shell environment above. This again causes user complaints about tools being unable to output anything
outside ASCII range when launched from the IDE. Resolved by adding LC_CTYPE variable to the map if it doesn't contain
explicitly set locale variables (LANG/LC_ALL/LC_CTYPE). See setCharsetVar(Map)
for details.
public static java.lang.String getValue(java.lang.String name)
getEnvironmentMap().get(name)
.
Returns value for the passed environment variable name, or null if no such variable found.getEnvironmentMap()
public static java.lang.String [] getEnvironment()
flattenEnvironment(getEnvironmentMap())
.
Returns an environment as an array of "NAME=VALUE" strings.getEnvironmentMap()
public static java.lang.String [] flattenEnvironment(java.util.Map<java.lang.String,java.lang.String> environment)
public static boolean isValidName(java.lang.String name)
ProcessEnvironment#validateVariable
(ProcessEnvironment#validateName
on Windows).public static boolean isValidValue(java.lang.String value)
ProcessEnvironment#validateValue
.isValidName(String)
public static java.util.List<java.lang.String> buildShellProcessCommand(java.lang.String shellScript, boolean isLogin, boolean isInteractive, boolean isCommand)
shellScript
path.shellScript
- path to the shell script, probably taken from environment variable SHELL
isLogin
- true iff it should be login shell, usually -l
parameterisInteractive
- true iff it should be interactive shell, usually -i
parameterisCommand
- true iff command should accept a command, instead of script name, usually -c
parameter/bin/bash -l -i -c
public static java.util.Map<java.lang.String,java.lang.String> parseEnv(java.lang.String... lines)
public static java.lang.String setLocaleEnv(java.util.Map<java.lang.String,java.lang.String> env, java.nio.charset.Charset charset)
public static void inlineParentOccurrences(java.util.Map<java.lang.String,java.lang.String> envs)
public static void inlineParentOccurrences(java.util.Map<java.lang.String,java.lang.String> envs, java.util.Map<java.lang.String,java.lang.String> parentEnv)