File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Using System.getenv() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using System.getenv()" Watch "Using System.getenv()" New topic

Using System.getenv()

ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
Hi all,

I have set some environment variables in my unix OS and would like to retrieve them in windows OS. Can I use System.getenv("env var") in my java code running in Windows to do this?

Or, is there any other way of obtaining the same?

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
The question doesn't seem to make sense: of course you can't read Unix environment variables on Windows.

If you mean "does getenv() work on Windows?", the answer is yes. Though it went through a period of being deprecated, it is now a proper interface again in the latest Java.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199


Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name, with a space in between. You can change your display name here. Thanks!

[Jess in Action][AskingGoodQuestions]
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
I have had trouble with the environmental variables in UNIX when accessing them through Java (i.e. using exec, or getenv). I found that java doesnt correctly read my environmental variables ! For example, a quick "echo $PATH" yields a string with 5 path values in it (/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/:/usr/local/bin/MyPrograms) but in java, my "Path" as displayed using process.exec has only 3 entries (/bin:/sbin:/usr/bin:/usr/sbin).

Is there a way to force java to obtain the "Path" using the same method that my "shells" use ? I've never really thought about the shells and where they get their environmental variables from, but it seems like they always seem to work !
gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
hi jay,
i don't think java is not able to read env variables properly. i just tried the same thing and got the following result

C:\ClarifyCRM12iSR1.04\Configurator\Common Files\CCAutomation;C:\ClarifyCRM12iSR
ogram Files\Java\jdk1.5.0_07\bin

using System.getenv("PATH")

which is exactly same as what i get with echo command.

there may be some other scenario in which you are getting this problem.

thanks,<br />gaurav abbi
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
Hmmm... do you think it is UNIX related ? Are there different PATH's for different processes in UNIX ?
I agree. Here's the link:
subject: Using System.getenv()
It's not a secret anymore!