Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to set LD_LIBRARY_PATH within the Java program?

1,649 views
Skip to first unread message

John

unread,
Apr 9, 2015, 11:28:26 AM4/9/15
to
Hi:

I need to set linux environment variable LD_LIBRARY_PATH within my Java program. The reason is that the Java program calls some linux program which dynamically links to other *.so files. I could do this on the terminal first:

export LD_LIBRARY_PATH=/fs/great/lib:${LD_LIBRARY_PATH}

For some reason, I don't want to do it this way. I want to do it within the Java program. I googled and saw this hacking way,

http://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java

People say it works great. But I don't understand his code, posted below. How to use it in my case. Thank you.

protected static void setEnv(Map<String, String> newenv)
{
try
{
Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
theEnvironmentField.setAccessible(true);
Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
env.putAll(newenv);
Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
theCaseInsensitiveEnvironmentField.setAccessible(true);
Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
cienv.putAll(newenv);
}
catch (NoSuchFieldException e)
{
try {
Class[] classes = Collections.class.getDeclaredClasses();
Map<String, String> env = System.getenv();
for(Class cl : classes) {
if("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
Field field = cl.getDeclaredField("m");
field.setAccessible(true);
Object obj = field.get(env);
Map<String, String> map = (Map<String, String>) obj;
map.clear();
map.putAll(newenv);
}
}
} catch (Exception e2) {
e2.printStackTrace();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}

Eric Sosman

unread,
Apr 9, 2015, 1:20:44 PM4/9/15
to
On 4/9/2015 11:28 AM, John wrote:
> Hi:
>
> I need to set linux environment variable LD_LIBRARY_PATH within my Java program. The reason is that the Java program calls some linux program which dynamically links to other *.so files. I could do this on the terminal first:
>
> export LD_LIBRARY_PATH=/fs/great/lib:${LD_LIBRARY_PATH}
>
> For some reason, I don't want to do it this way. I want to do it within the Java program. I googled and saw this hacking way,
>
> http://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java
>
> People say it works great. But I don't understand his code, posted below. How to use it in my case. Thank you.
>[...]

People who say "it works great" must be using it for purposes
rather different from yours, or must have a different notion of
what "great" means. The code (if it works at all) will modify
some Java maps in Java's memory, but will not affect the "real"
environment that lives "out there" in Linux or Windows or whatever.
It will change Java's read-only (supposedly) copy of the external
environment, but won't change that external environment -- and since
the library loader will consult the external environment's value of
LD_LIBRARY_PATH, changing Java's internal copy won't help you.

You might, I guess, inspect LD_LIBRARY_PATH yourself, and if it
isn't what you want you could set up a ProcessBuilder with the
desired value and re-launch your program as a sub-process.

--
eso...@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

A. Bolmarcich

unread,
Apr 9, 2015, 5:18:01 PM4/9/15
to
On 2015-04-09, John <xs...@yahoo.com> wrote:
> I need to set linux environment variable LD_LIBRARY_PATH within my Java program. The reason is that the Java program calls some linux program which dynamically links to other *.so files. I could do this on the terminal first:
>
> export LD_LIBRARY_PATH=/fs/great/lib:${LD_LIBRARY_PATH}
>
> For some reason, I don't want to do it this way. I want to do it within the Java program. I googled and saw this hacking way,
[snip]

Consider using a non-hacking way, such as replacing the execution of the linux
programs with the execution of a shell that sets the environment and then runs
the linux program.

Andreas Leitgeb

unread,
Apr 10, 2015, 7:20:14 AM4/10/15
to
A. Bolmarcich <agg...@earl-grey.cloud9.net> wrote:
> On 2015-04-09, John <xs...@yahoo.com> wrote:
>> I need to set linux environment variable LD_LIBRARY_PATH within my Java program. The reason is that the Java program calls some linux program which dynamically links to other *.so files. I could do this on the terminal first:
>> export LD_LIBRARY_PATH=/fs/great/lib:${LD_LIBRARY_PATH}
> Consider using a non-hacking way, such as replacing the execution of the linux
> programs with the execution of a shell that sets the environment and then runs
> the linux program.

This might be even simpler:
If you(John, the OP) currently call a program "foo" with 2 arguments:
foo bar baz
then you instead call the unix utility "env" with these arguments:
env LD_LIBRARY_PATH=... foo bar baz
where you replace the "..." with the new content for the variable

Otoh., using a shell-script (as A. Bolmarcich suggested) will give you
more flexibility if you want to change more of the environment later
without changing re-compiling and re-deploying the Java code.

0 new messages