A process can only set environment variables of itself and for processes it will spawn in future. A process cannot set the environment variables of already running processes.
You might have already noticed that when you were setting environment variables manually, globally in the system. They will not affect instances of processes which are already running, like an already running cmd.exe
or an already running bash
. You might also have noticed, that if you set an environment variable that way, that whether or not a new process gets the new environment variable setting depends on how the new process is started. The default behavior is that a process is started with a copy of the environment of its parent process, which is the process that starts the new process.
As a simple explanation, you could say there are root processes and child processes. root processes get the environment settings from the global settings, child processes inherit the environment settings from their parent processes.
The question is what do you want to achieve with setting the environment? I could think of at least three different things that you could want to achieve:
- Set the environment globally, as part of an installer.
- Set the environment for the currently running JVM.
- Set the environment for a process that you will be starting.
- Set the environment for the calling process directly (not possible!).
- Set the environment for the calling process indirectly.
Setting the environment globally, as part of an installer
This is highly system-specific.
On UNIX, this topic is actually avoided.
Programs would rather provide wrapper scripts that set the environment instead of setting global environment variables.
The philosophy in UNIX is that usually environment variables are only used in cases where the variable would be useful for more than just one process.
Examples for such varaibles are PATH
and EDITOR
.
On Windows, you would probably call regedit
to modify the environment.
Setting the environment of the currently running JVM
There is no API for setting the environment of the currently running JVM, so you would have to use JNI for this. However, be advised, that the fact that there is no API for this is for good reasons, and part of these reasons might be that the JVM doesn't want its environment be arbitrarily changed by some Java code.
Setting the environment for a process that will be started
When you start a process using one of the Runtime.exec()
methods, you can actually provide the environment that you like.
If you want to start a process with a modified environment, the best way would be to use ProcessBuilder
. It provides a method environment() for modifying the environment for the new process.
Setting the environment for the calling process directly
If you want to implement the set
command in Java, forget it, it is not possible. set
is not a program, it's an internal command of the shell, i.e. cmd.exe
. Because of the explanation above, it wouldn't work otherwise.
Setting the environment for the calling process indirectly
You can set the environment for the calling process indirectly - if the calling process cooperates. If your calling process is cmd.exe
or sh
, you could have your Java program generate a temporary batch file or shell script, and then have the calling cmd.exe
or sh
execute that batch file or shell script.