RE: Accessing Environment Variables
"Shankar Unni" <[email protected]> Sun, 13 Feb 2005 12:13:18 -0800
| Newsgroups | gmane.comp.java.beanshell.devel |
|---|---|
| Message-ID | <[email protected]> |
In fact, this might make an interesting standard function script as follows:
source("initenv.bsh");
loadEnv("env");
// Now you can access environment vars as env.ENVVAR.
print(System.getProperty("env.PATH"));
> -----Original Message-----
> From: beanshell-developers-admin-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> [mailto:beanshell-developers-admin-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org] On
> Behalf Of Shankar Unni
> Sent: Sunday, February 13, 2005 11:56 AM
> To: 'Patrick Niemeyer'; [email protected]
> Cc: beanshell-developers-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org; 'Nicholas Smith'
> Subject: RE: [Beanshell-dev] Accessing Environment Variables
>
> > >> 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
> _______________________________________________
> Beanshell-developers mailing list
> Beanshell-developers-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> https://lists.sourceforge.net/lists/listinfo/beanshell-developers
>
initenv.bsh
(application/octet-stream, 1 KB) - not displayed