This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

putenv ?

 
eileen keeney
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 . . .









































nothing.

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
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
eileen keeney
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
eileen keeney wrote: . . . But I found an easier solution . . .
Well done
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic