> I know this isn't a direct answer to your original question but i don't want
> to spend the next hour reading umpteen threads of information and then
> trying to paraphrase it all; it makes a lot more sense if you read the
> information for yourself since you know best what you are trying to do and
> what your environment is.
No problem with that, unfortunately this thread and the following do
not give me any answers. My problem is not to get the value of a
particular environment variable, but all the values of all environment
variables.
But I might be heading in the wrong direction.
My problem is: I need to exec an external process, and I need to add an
environment variable before doing so.
The problem is that if I use Runtime.exec(String), my environment is
the old one without my variable, and If I put an array with my new
environment variable, I lost my old environment.
I do not find a way, in Java 1.4, to launch an external command with a
enriched environment.
I can do it in Java 5 thanks to the System.getenv() method and that's
why I asked how to replace this method in Java 1.4, but if this is not
possible, is their another way to solve my problem?
BTW, I do not want to use JNI, because I do want to be cross-platform.
Thanks.

Signature
Benjamin Lerman
Rhino - 10 May 2006 00:17 GMT
>> I know this isn't a direct answer to your original question but i don't
>> want
[quoted text clipped - 26 lines]
>
> BTW, I do not want to use JNI, because I do want to be cross-platform.
I'm not sure what to suggest other than what others have suggested in this
thread and the older ones I cited.
Sorry,
--
Rhino
Chris Uppal - 10 May 2006 09:24 GMT
> My problem is: I need to exec an external process, and I need to add an
> environment variable before doing so.
This is an /inherently/ system-dependent operation. So you can forget about
cross-platform portability (the only reason it can be "portable" in 1.5 is that
the JVM/library itself contains platform-specific code).
The question is not /whether/ you are going to be able to use cross-platform
code, but /what/ platform-specific code you will have to write. So it's your
choice...
-- chris
Gordon Beaton - 10 May 2006 13:25 GMT
> My problem is not to get the value of a particular environment
> variable, but all the values of all environment variables.
This can be done by running /usr/bin/printenv or similar external
program with Runtime.exec(), and parsing the values from the
InputStream. One convenient way is with Properties.load():
Properties props = new Properties();
Process p = Runtime.getRuntime().exec("/usr/bin/printenv");
props.load(p.getInputStream);
> My problem is: I need to exec an external process, and I need to add
> an environment variable before doing so.
When you run a command in a Unix shell you can modify the environment
locally for that process, for example by adding variable assignments
to the command line:
bash$ FOO=baz /usr/bin/printenv FOO
baz
bash$ FOO=gurka /usr/bin/printenv FOO
gurka
bash$ /usr/bin/printenv FOO
(nothing)
bash$
You can use the same technique when you invoke Runtime.exec():
String foo = "someValue";
String[] cmd = {
"/bin/sh",
"-c",
"FOO=" + foo + " /some/path/myCommand"
}
Process p = Runtime.getRuntime().exec(cmd);
(etc)
Another way is to wrap the external program in a shell script or batch
file that expects the extra environment variables on the command line,
and can set them before invoking the program itself.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Juha Laiho - 16 May 2006 20:35 GMT
Benjamin Lerman <news+news-free@ambre.net> said:
> My problem is: I need to exec an external process, and I need to add an
>environment variable before doing so.
>
> The problem is that if I use Runtime.exec(String), my environment is
>the old one without my variable, and If I put an array with my new
>environment variable, I lost my old environment.
If you're working with just Unix platforms, you could use /usr/bin/env
as a helper to start the external application, like:
/usr/bin/env NEWVARIABLE=value /path/to/extcomm arg1 arg2

Signature
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)