This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes putenv ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "putenv ?" Watch "putenv ?" New topic

putenv ?

eileen keeney
Ranch Hand

Joined: May 04, 2009
Posts: 51
Does Java have a putenv method? Does it exist in a standard library? (I get nothing for this, searching the Sun pages).

I see other references to putenv, in various places on the web, but nothing I can get to work.

If putenv exists, Which library? (Or is it only in a specific os library, maybe for an os I am not using?)

How is it used? (maybe an example).

Any recommendations on setting an environment variable.
(as for why, I later call another program, from within the java program, that is already coded to check the environment variable. I can not change this other program.)

I did manage to do a system call to "/usr/bin/ksh . /home/account/.profile" to not error (I had to include the /user/bin/ksh to get it to not tell me it did not understand the .)
However, this still failed to get the values passed up, so that later when I did a System.getenv it would have the correct values.
However, if I . the same .profile script, before running my java code, the System.getenv does retrieve the correct values, therefore my getenv command is executing correctly.

If I can verify that putenv does not exist (or does not exist for the OS I am using) then I can at least stop mucking with trying to get it to work, and move on to another possible solution.

Thank you
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38107
Go to the API documentation, click on the "index" link at the top of the page, then "P", then scroll down and after putD and before putF . . .

Well, I found . . .


Don't know how to set an environment variable from inside a Java application. You might try the Properties class; don't know whether that provides what you are looking for.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19656

My first guess was System.getenv(), but the returned Map is unmodifiable. I guess it's not possible without some native code.

That said, Campbell was right about the Properties class. For internal use, System.getProperties() should be sufficient. And yes, that one does have a setProperty method.

If you need to set the environment variable only for a process you're launching from Java code, then you can use the following:
If you're using ProcessBuilder it's even easier:

How To Ask Questions How To Answer Questions
eileen keeney
Ranch Hand

Joined: May 04, 2009
Posts: 51
Thanks Rob, that is what I was looking for.

But I found an easier solution to my problem.
I can run a shell script with the needed values in them, when I start tomcat, and all of my servlets know the values.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38107
eileen keeney wrote: . . . But I found an easier solution . . .
Well done
subject: putenv ?
Similar Threads
Get environment variables
startup.bat stops itself
accessing ojdbc14.jar from a servlet?
getRuntime(command, envp) not sourcing environment