Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Setting environment variables

Reply
Thread Tools

Setting environment variables

 
 
Steve
Guest
Posts: n/a
 
      10-21-2003
Hi,
I need to set some environment variables within a Java app so that some JNI
code called later can access them. I don't know the values of these
variables a priori, so I can't set them before running the app. Looking
through the archives, it seems this may be impossible, but not definitively
so. But I thought I'd ask in case someone has any ideas. I've tried
writing a JNI method that calls putenv(), but putenv() fails (perhaps the
JVM is prohibiting any environment changes?). Any ideas?

Thanks,
Steve

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      10-21-2003
On Tue, 21 Oct 2003 14:52:20 GMT, Steve wrote:
> I need to set some environment variables within a Java app so that
> some JNI code called later can access them. I don't know the values
> of these variables a priori, so I can't set them before running the
> app. Looking through the archives, it seems this may be impossible,
> but not definitively so. But I thought I'd ask in case someone has
> any ideas. I've tried writing a JNI method that calls putenv(), but
> putenv() fails (perhaps the JVM is prohibiting any environment
> changes?). Any ideas?


Environment variables are a silly way to communicate *within* the
process. Why don't you pass the information directly to the native
methods (via the argument list)?

That said, the JVM does not prevent putenv() from working. Do you mean
that putenv() actually fails (i.e. it returns -1), or that getenv()
doesn't find the variables?

If putenv() really is failing, check the value of errno to see the
reason.

Otherwise, perhaps there is something else wrong with your code or the
way you test it. For example, the string that you pass to putenv()
must stay in scope, unaltered.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      10-21-2003
Gordon Beaton wrote:

> That said, the JVM does not prevent putenv() from working. Do you mean
> that putenv() actually fails (i.e. it returns -1), or that getenv()
> doesn't find the variables?
>
> If putenv() really is failing, check the value of errno to see the
> reason.
>
> Otherwise, perhaps there is something else wrong with your code or the
> way you test it.


Perhaps it "fails" because it's a Unix system, where environment variables
are process-specific. Which is one reason why one shouldn't use them in
a Java program.

 
Reply With Quote
 
Doyle
Guest
Posts: n/a
 
      10-21-2003
Steve <(E-Mail Removed)> wrote in message news:<CKblb.833484$YN5.923544@sccrnsc01>...
> Hi,
> I need to set some environment variables within a Java app so that some JNI
> code called later can access them. I don't know the values of these
> variables a priori, so I can't set them before running the app. Looking
> through the archives, it seems this may be impossible, but not definitively
> so. But I thought I'd ask in case someone has any ideas. I've tried
> writing a JNI method that calls putenv(), but putenv() fails (perhaps the
> JVM is prohibiting any environment changes?). Any ideas?


You should be able to use the java.lang.System method get/setProperty
to access the system properties. From the documentation:

"public static String setProperty(String key, String value)
Sets the system property indicated by the specified key.
First, if a security manager exists, its
SecurityManager.checkPermission method is called with a
PropertyPermission(key, "write") permission. This may result in a
SecurityException being thrown. If no exception is thrown, the
specified property is set to the given value. "

mypetrock
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      10-21-2003
On 21 Oct 2003 11:17:27 -0700, Doyle wrote:
> You should be able to use the java.lang.System method get/setProperty
> to access the system properties.


Except that the System properties have absolutely nothing to do with
the process' environment variables.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-21-2003
On Tue, 21 Oct 2003 14:52:20 GMT, Steve <(E-Mail Removed)> wrote
or quoted :

>I need to set some environment variables within a Java app so that some JNI
>code called later can access them. I don't know the values of these
>variables a priori, so I can't set them before running the app. Looking
>through the archives, it seems this may be impossible, but not definitively
>so. But I thought I'd ask in case someone has any ideas. I've tried
>writing a JNI method that calls putenv(), but putenv() fails (perhaps the
>JVM is prohibiting any environment changes?). Any ideas?


the JVM knows nothing about your putenv. I don't see how it could
interfere. Try writing and debugging your code standalone before
hooking it into JNI.

for other options see http://mindprod.com/jgloss/environment.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
lightsedge lightsedge is offline
Junior Member
Join Date: May 2007
Posts: 1
 
      05-01-2007
check out the ProcessBuilder class for Java 5 and up
 
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
Setting an environment variable from another environment variable marcwentink@hotmail.com Java 5 04-04-2007 10:39 PM
problem with setting Java environment variables in Linux Leo Java 2 03-21-2006 04:49 PM
Displaying and setting current environment variables from ANT ? Ulf Meinhardt Java 0 10-13-2004 09:19 PM
Setting Environment Variables Rick Kasten Perl 2 07-07-2004 07:43 PM
how to set .net environment variables - a one time setting by the administrator =?Utf-8?B?YnNmbDQ0?= ASP .Net 0 05-12-2004 12:56 PM



Advertisments