RE: Accessing Environment Variables

"Shankar Unni" <[email protected]> Sun, 13 Feb 2005 11:55:37 -0800
Newsgroups gmane.comp.java.beanshell.devel
Message-ID <[email protected]>
> >> java.lang.Error: getenv no longer supported, use 
> >> properties instead:
> >> PATH at java.lang.System.getenv(java.lang.String)

For 1.4, the only workaround is to do something like what Ant does for
environment access (the "env" command - run a native command, read the
output, and set system properties).  

Since every Java programmer eventually runs into this, Sun "graciously" put
the functionality back (after being persuaded that this was not "os-specific
functionality").

The basic idea behind the native command hack (at least for Windows and
Unix-like environments, including MacOSX) is to:

* Check System.pathSeparatorChar. If ";", run "cmd /c set" (via
System.exec()), else run just "set".
* Read and parse the output, reading line by line, and set system properties
with those values (maybe using some prefix naming convention). 

Some care has to be taken about variables like IFS (whose value contains a
newline).

This logic suffices for 99.9% of the cases (except maybe if you are running
Java in some thoroughly alien environment which is not Windows *and* where
there is no /bin/sh).



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click