Setting environment variables from java

K

Karim

Hi,

Under linux, I have java code that runs a c executable using
Runtime.getRuntime().exec()

Before running that I have to set the LD_LIBRARY_PATH system
variable..

I tried Runtime.getRuntime().exec("setenv ....")
Runtime.getRuntime().exec("export ... ")

and I also tried System.setProperty();

and non of the above seem to do the trick..

any hints?

Thanks
 
M

Martin Gregorie

Karim said:
Hi,

Under linux, I have java code that runs a c executable using
Runtime.getRuntime().exec()

Before running that I have to set the LD_LIBRARY_PATH system
variable..

I tried Runtime.getRuntime().exec("setenv ....")
Runtime.getRuntime().exec("export ... ")

and I also tried System.setProperty();

and non of the above seem to do the trick..
Runtime.getRuntime().exec will start a separate shell each tine its
called, so nothing do in one call can affect the environment for a
subsequent call. I think your best choices are:

- run a script that sets the environment before running the program
- change .profile in the user you're running under to set
LD_LIBRARY_PATH
- recompile the program with static linking

....listed with best choice first.
 
G

Gordon Beaton

Runtime.getRuntime().exec will start a separate shell each tine its
called, so nothing do in one call can affect the environment for a
subsequent call.

However at least one version of Runtime.exec() lets you specify the
environment for the child process being invoked. Combine that with
System.getEnv() and you can make changes to the default enviromnent
before passing it to Runtime.exec().

Or use ProcessBuilder.environment().

/gordon

--
 
N

Nigel Wade

Martin said:
Runtime.getRuntime().exec will start a separate shell each tine its
called, so nothing do in one call can affect the environment for a
subsequent call. I think your best choices are:

- run a script that sets the environment before running the program
- change .profile in the user you're running under to set
LD_LIBRARY_PATH
- recompile the program with static linking

...listed with best choice first.

or do what Robert suggested when this post was sent yesterday...
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top