Setting environment variables

Discussion in 'Java' started by Steve, Oct 21, 2003.

  1. Steve

    Steve Guest

    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
     
    Steve, Oct 21, 2003
    #1
    1. Advertising

  2. 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
     
    Gordon Beaton, Oct 21, 2003
    #2
    1. Advertising

  3. 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.
     
    Michael Borgwardt, Oct 21, 2003
    #3
  4. Steve

    Doyle Guest

    Steve <> 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
     
    Doyle, Oct 21, 2003
    #4
  5. 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
     
    Gordon Beaton, Oct 21, 2003
    #5
  6. Steve

    Roedy Green Guest

    On Tue, 21 Oct 2003 14:52:20 GMT, Steve <> 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.
     
    Roedy Green, Oct 21, 2003
    #6
  7. Steve

    lightsedge

    Joined:
    May 1, 2007
    Messages:
    1
    check out the ProcessBuilder class for Java 5 and up
     
    lightsedge, May 1, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Kasten

    Setting Environment Variables

    Rick Kasten, Jul 7, 2004, in forum: Perl
    Replies:
    2
    Views:
    607
    Howard
    Jul 7, 2004
  2. =?Utf-8?B?YnNmbDQ0?=
    Replies:
    0
    Views:
    496
    =?Utf-8?B?YnNmbDQ0?=
    May 12, 2004
  3. Ulf Meinhardt
    Replies:
    0
    Views:
    572
    Ulf Meinhardt
    Oct 13, 2004
  4. Leo
    Replies:
    2
    Views:
    3,007
    Gordon Beaton
    Mar 21, 2006
  5. Replies:
    5
    Views:
    675
Loading...

Share This Page