Stefan said:
Thanks. It works. Is there a replacement for this method that is not
deprecated,
The method should never have been deprecated, and Sun corrected this at
some point in time. At least, it is not deprecated in 1.5.
or is it "forbidden" to use environment vars in java?
No, why should it? Some people have the argument that environment
variables don't exist on all platforms, so such a method is not a good
idea. However, this was not a really good reason to deprecate the method.
It is possible to provide an entirely conforming implementation on
systems which don't have environment variables. The method is supposed
to return null if a variable does not exist (which can happen on
platforms with environment variables, too). So a compliant
implementation on systems without any environment variable would be to
always return null - which then correctly indicates that the variable
does not exist.
Alternatively, VM implementers could provide means to set "environment
variables" for a particular VM, e.g. by making the System.Properties (or
some other set of properties) available via the getenv() call, too. This
would be compliant, because the API spec does not provide a clear
definition of what an "environment variable" is supposed to be. Quote
"An environment variable is a system-dependent external variable that
has a string value."
There are other - Java independent - reasons why using environment
variables are often not a good idea. Controlling application behavior or
configuration via environment variables is a no-no, because this is
usually not what a user expects. Users usually don't want their
applications to behave differently just because the environment has
changed. Instead, application behavior is expected to be deterministic,
only depending on the input explicitly provided to the application.
Of course, there are exceptions, e.g. Unix users expect that the default
printer name is picked up from the PRINTER environment variable, or the
default editor form the EDITOR or VISUAL variable.
But in general, application configuration should be done via command
line options or some configuration file, not via some "hidden"
environment variables.
/Thomas