Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>If my intuition is correct</strong>, and you actually want to modify the environment for the benefit of a spawned (forked) sub-process (<code>Runtime.getRuntime().exec()</code>), then use <a href="http://java.sun.com/javase/6/docs/api/java/lang/ProcessBuilder.html" rel="noreferrer">ProcessBuilder</a> instead of <code>exec()</code>. You can build a custom environment via your <code>ProcessBuilder</code> instance's <a href="http://java.sun.com/javase/6/docs/api/java/lang/ProcessBuilder.html#environment()" rel="noreferrer">environment()</a> method.</p> <p>If this is <em>not</em> what you are trying to achieve then kindly disregard this answer.</p> <hr> <p><strong>UPDATE</strong></p> <p>The answer to your three updated, specific questions is as follows:</p> <ol> <li>Can we modify the environment of the current process? <ul> <li><strong>Not easily</strong>. Depends whether you want to change the process' environment, to change the value(s) returned by <code>System.getenv()</code> in the same JVM, or both.</li> <li>As Greg Hewgill pointed out, to change the current process' environment you can call <code>setenv</code> or its platform-specific equivalent via JNI. You may also employ the extremely convoluted method from <em>point 2</em> 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 by <code>System.getenv()</code>, as the environment is more often than not cached at virtual machine startup in a <code>java.util.Map</code> (or equivalent.)</li> <li>To change the JVM's cached copy of the environment, when a cache is used (see the source code in <code>System.java</code> in whichever JVM distribution you will be using to deploy), you may try hacking the implementation (via class loading order, <a href="http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html" rel="noreferrer">reflection</a>, or <a href="http://java.sun.com/javase/6/docs/api/java/lang/instrument/Instrumentation.html" rel="noreferrer">instrumentation</a>.) In the case of SUN's v1.6 JVM, for example, the environment cache is managed by the undocumented <code>ProcessEnvironment</code> class (which you can patch.)</li> </ul></li> <li>Can we modify the environment of the parent process? <ul> <li><strong>Extremely difficult, and highly non-portable</strong>. If you absolutely and imperatively have to, there are very specific hacks that you can employ: <ul> <li>Windows: <a href="http://www.codeproject.com/KB/threads/DynEnvVar.aspx" rel="noreferrer">Dynamically Add/Edit Environment variables of Remote process</a></li> <li>*nix: <a href="https://stackoverflow.com/questions/205064/is-there-a-way-to-change-another-processs-environment-variables">Is there a way to change another process’s environment variables?</a> -- this is a performance killer, as any process instrumented by <code>gdb</code> will be suspended for a non-zero amount of time.</li> </ul></li> </ul></li> <li>Can we modify the environment of the child process? <ul> <li><strong>Yes</strong>, through <code>ProcessBuilder</code> when spawning the process.</li> <li>If the process has already been spawned when the environment alteration is required, you need <em>method 2</em> above (or some equally convoluted method, such as code-injection at spawn time, ulteriorly controlled through e.g. socket by the parent process.)</li> </ul></li> </ol> <p>Note that all methods above, except for the one involving <code>ProcessBuilder</code>, are brittle, error prone, non-portable to various degrees, and prone to race conditions in multi-threaded environments.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload