Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Environment variable

Reply
Thread Tools

Environment variable

 
 
Stefan Poehn
Guest
Posts: n/a
 
      11-16-2004
Hi

is it possible to get an enviroment variable, e.g. $PATH, in a java
application in windows?

Thanks
Stefan


 
Reply With Quote
 
 
 
 
Thomas S.
Guest
Posts: n/a
 
      11-16-2004
Stefan Poehn wrote:
> is it possible to get an enviroment variable, e.g. $PATH, in a java
> application in windows?


java.lang.System.getProperty() should do it.

Thomas
 
Reply With Quote
 
 
 
 
Stefan Poehn
Guest
Posts: n/a
 
      11-16-2004
"Thomas S." <> schrieb im Newsbeitrag
news:...
> Stefan Poehn wrote:
>> is it possible to get an enviroment variable, e.g. $PATH, in a java
>> application in windows?

>
> java.lang.System.getProperty() should do it.
>


No. You can retrieve the environment _for the jvm_, not for the system, like
$PATH. System.getProperty("$PATH");
System.getProperty("%PATH%");System.getProperty("P ATH"); all return null.

> Thomas



 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      11-16-2004
Stefan Poehn wrote:
> No. You can retrieve the environment _for the jvm_, not for the system, like
> $PATH. System.getProperty("$PATH");
> System.getProperty("%PATH%");System.getProperty("P ATH"); all return null.


System.getenv()

Was AFAIR deprecated in some Java versions, but has been "un-deprecated"
for some time.

/Thomas
 
Reply With Quote
 
Stefan Poehn
Guest
Posts: n/a
 
      11-17-2004
"Thomas Weidenfeller" <> schrieb im Newsbeitrag
news:cndd7t$9o1$...
>
> System.getenv()
>
> Was AFAIR deprecated in some Java versions, but has been "un-deprecated"
> for some time.
>


Thanks. It works. Is there a replacement for this method that is not
deprecated, or is it "forbidden" to use environment vars in java?

> /Thomas



 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      11-17-2004
Stefan Poehn wrote:
>>System.getenv()
>>
>>Was AFAIR deprecated in some Java versions, but has been "un-deprecated"
>>for some time.
>>

>
>
> Thanks. It works. Is there a replacement for this method that is not
> deprecated,


No.

> or is it "forbidden" to use environment vars in java?


It is stringly discouraged, since it's not portable. Some systems don't
have environment variables at all (such as pre-X MacOS), and they don't
behave the same on all systems that do.
 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      11-17-2004
Stefan Poehn wrote:
> 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
 
Reply With Quote
 
Stefan Poehn
Guest
Posts: n/a
 
      11-17-2004
"Thomas Weidenfeller" <> schrieb im Newsbeitrag
news:cnfvg0$jnm$...
> [...]
> 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.
>


I can explain in simple words why we want to have an environment variable:
every developer has another folder structure on his harddisk. If you need
the absolute path to the project, you have to store this path outside your
program. This can be done with environment vars or registry entries, and I
really do not want to use the registry. (We are working on Windows only and
do not need to support other platforms).

> But in general, application configuration should be done via command line
> options or some configuration file, not via some "hidden" environment
> variables.
>
> /Thomas



 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      11-17-2004
On Wed, 17 Nov 2004 18:28:06 +0100, Stefan Poehn wrote:

> I can explain in simple words why we want to have an environment variable:
> every developer has another folder structure on his harddisk. If you need
> the absolute path to the project, you have to store this path outside your
> program. This can be done with environment vars or registry entries, ..


...or <http://www.physci.org/codes/javafaq.jsp#path>.

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      11-18-2004
Stefan Poehn wrote:
> I can explain in simple words why we want to have an environment variable:
> every developer has another folder structure on his harddisk. If you need
> the absolute path to the project, you have to store this path outside your
> program. This can be done with environment vars or registry entries, and I
> really do not want to use the registry. (We are working on Windows only and
> do not need to support other platforms).


As I wrote, using environment variables for such cases is bad.

/Thomas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
70-284 Lab Environment, Need Virtual Environment brooklynbridge508@hotmail.com MCSA 4 05-02-2007 09:49 AM
Setting an environment variable from another environment variable marcwentink@hotmail.com Java 5 04-04-2007 10:39 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM
Set environment variable in setup application Tom ASP .Net 0 02-06-2004 06:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57