Many times it happens as part of integration or performance tests.
And here's the way to overcome it:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void setEnv(String name, String value) { | |
try { | |
final Class<?> envClass = Class.forName("java.lang.ProcessEnvironment"); | |
final Class<?> uMapClass = Class.forName("java.util.Collections$UnmodifiableMap"); | |
final Field mField = uMapClass.getDeclaredField("m"); | |
mField.setAccessible(true); | |
final Field theUnmodifiableEnvironment = envClass.getDeclaredField("theUnmodifiableEnvironment"); | |
theUnmodifiableEnvironment.setAccessible(true); | |
final Map oldMap = (Map) theUnmodifiableEnvironment.get(System.getenv()); | |
final HashMap newMap = new HashMap(oldMap); | |
newMap.put(name, value); | |
mField.set(System.getenv(), newMap); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} |