Setting environment variables

S

Steve

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
 
G

Gordon Beaton

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
 
M

Michael Borgwardt

Gordon said:
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.
 
D

Doyle

Steve said:
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
 
G

Gordon Beaton

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
 
R

Roedy Green

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top