Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

19
  • 3
    Will this only change in memory, or actually change the entire environment variable in the system? Commented Jun 25, 2012 at 7:12
  • 52
    This will only change the environment variable in memory. This is good for testing, because you can set the environment variable as necessary for your test, but leave the envs in the system as they are. In fact, I would strongly discourage anyone from using this code for any other purpose than testing. This code is evil ;-) Commented Jun 25, 2012 at 8:09
  • 16
    As an FYI, the JVM creates a copy of the environment variables when it starts. This will edit that copy, not the environment variables for the parent process that started the JVM. Commented Aug 15, 2012 at 14:12
  • 5
    Sure, import java.lang.reflect.Field; Commented Jan 21, 2013 at 9:44
  • 6
    Doesn't work in OpenJDK 17. And the corresponding implementation differs from Linux to Windows. Commented Nov 11, 2022 at 8:53