Is it possible to set an environment variable at runtime from a Java application? In Java 1.5 java.lang.System class there is the getenv() method, I would only need a setenv() method...
Is it possible to modify the environment variables in the java process itself; not in the child process.
Is it possible to achieve it through JNI? And how would that work?
Thanks.
EDIT: Ok let me put it this way - Can we do the following with Java. Please answer.
- Can we modify the environment of the current process?
- Can we modify the environment of the parent process?
- Can we modify the environment of the child process?
Hemal Pandya has answered that "You can modify the environment of current and child processes but not of the parent process that spawned this process." Do you agree with this ?
-
I don't think so, at least not purely in Java, but why do you need to do this? In Java it's preferable to use properties via
System.getProperties(), which you can modify.If you really must, I'm sure you could wrap the C
setenvfunction in a JNI call - in fact, I wouldn't be surprised if someone has done so already. I don't know the details of the code, though. -
If my intuition is correct, and you actually want to modify the environment for the benefit of a spawned (forked) sub-process (
Runtime.getRuntime().exec()), then use ProcessBuilder instead ofexec(). You can build a custom environment via yourProcessBuilderinstance's environment() method.If this is not what you are trying to achieve then kindly disregard this answer.
UPDATE
The answer to your three updated, specific questions is as follows:
- Can we modify the environment of the current process?
- Not easily. Depends whether you want to change the process' environment, to change the value(s) returned by
System.getenv()in the same JVM, or both. - As Greg Hewgill pointed out, to change the current process' environment you can call
setenvor its platform-specific equivalent via JNI. You may also employ the extremely convoluted method from point 2 below, which works for any process (provided you have the permissions.) However, be aware that in most JVMs this change might never be reflected in the values returned bySystem.getenv(), as the environment is more often than not cached at virtual machine startup in ajava.util.Map(or equivalent.) - To change the JVM's cached copy of the environment, when a cache is used (see the source code in
System.javain whichever JVM distribution you will be using to deploy), you may try hacking the implementation (via class loading order, reflection, or instrumentation.) In the case of SUN's v1.6 JVM, for example, the environment cache is managed by the undocumentedProcessEnvironmentclass (which you can patch.)
- Not easily. Depends whether you want to change the process' environment, to change the value(s) returned by
- Can we modify the environment of the parent process?
- Extremely difficult, and highly non-portable. If you absolutely and imperatively have to, there are very specific hacks that you can employ:
- Windows: Dynamically Add/Edit Environment variables of Remote process
- *nix: Is there a way to change another process’s environment variables? -- this is a performance killer, as any process instrumented by
gdbwill be suspended for a non-zero amount of time.
- Extremely difficult, and highly non-portable. If you absolutely and imperatively have to, there are very specific hacks that you can employ:
- Can we modify the environment of the child process?
- Yes, through
ProcessBuilderwhen spawning the process. - If the process has already been spawned when the environment alteration is required, you need method 2 above (or some equally convoluted method, such as code-injection at spawn time, ulteriorly controlled through e.g. socket by the parent process.)
- Yes, through
Note that all methods above, except for the one involving
ProcessBuilder, are brittle, error prone, non-portable to various degrees, and prone to race conditions in multi-threaded environments.Cheers, V.
Greg Hewgill : I believe your intuition is correct, Vicky has been trying to solve this problem for weeks. See the related links in the question comments above. - Can we modify the environment of the current process?
-
This is definitely not possible in a Windows environment, not sure about Linux/Unix.
-
You can modify the environment of current and child processes but not of the parent process that spawned this process.
-
In response to your updated question:
- Can we modify the environment of the current process?
Yes, if you use JNI to callsetenv()or something. You probably don't need to do this though, and it may not work in all situations. - Can we modify the environment of the parent process?
No. - Can we modify the environment of the child process?
Yes, usingProcessBuilder.
- Can we modify the environment of the current process?
0 comments:
Post a Comment