EVALUATION
A non-jni workaround would be possible if we could get the parent process'
environment. See bug 4642629. In that case, we could get the environment,
modify it, and use the existing Runtime APIs.
-- iag@sfbay 2002-08-28
An even cleaner solution to this problem would be to define two new methods: A
new System.getenv() method that returns a Map<String,String> of the environment
variables of the JRE process, and a new Runtime.exec method that takes a
command and an environment-variable map.
-- ###@###.### 2003/1/31
The smallest change to the API that would satisfy the user's desires is
the addition of
Map<String,String> System.environment()
and then adding
Process Runtime.exec(String[]args,Map<String,String>environ, File dir)
The obvious problem is that Runtime.exec is already horribly overloaded,
with four methods that take a String envp[] style environment.
public Process exec(String cmd, String envp[])
public Process exec(String command, String envp[], File dir)
public Process exec(String cmdarray[], String envp[])
public Process exec(String cmdarray[], String envp[], File dir)
If we were to be consistent, we would add 4 new methods that mirror the
above. The situation will get worse in the very likely event that
we will want to add new attributes that new processes can have,
for example:
- redirect stdio streams
- change process priority
- create daemon processes
- provide byte-level access to process properties like working directory.
An example of what I fear is the huge list of parameters to the
Windows CreateProcess function.
A better way is to have a separate class to capture nascent
process attributes.
class ProcessBuilder {
public ProcessBuilder(String command...)
public Map<String,String> environment()
public void setWorkingDirectory(File dir)
}
Unix folks can can think of this class as the state of a process after
fork() and before exec().
An advantage of this design is that the map returned by environment()
can be designed in such a way that the exact byte sequences of environment
variables not modified by the user program will be perfectly preserved
in any child process. There should be no setEnvironment(Map<String,String>)
method.
While implementing class ProcessBuilder, we also added a method
ProcessBuilder.redirectErrorStream() which addresses the
functionality described in this rfe:
4480528: (process) Need combined stderr/stdout streams
###@###.### 2003-08-03
|