0

Under Linux, I have a Java process and one of it's plugins depends on libgtk-*.so shared library. Currently we use $LD_LIBRARY_PATH or $LD_PRELOAD to set the path to this shared library. But if the Java process opens subprocesses, then they will also have those envs set, which lead to unwanted results.

In any other programming language (such as Python), I would just unset those two envs once the Java process is started running. But it looks like Java does not let you a way to modify envs in runtime.

Is it possible to provide the shared library as one of the Java arguments? Any other way to set this shared library only for the Java process and not for any subprocess?

vesii
  • 2,760
  • 4
  • 25
  • 71
  • That linked question is pretty old. I don’t think it’s correct anymore. I just successfully started subprocesses with environment variables removed, by invoking `processBuilder.environment().remove("LD_LIBRARY_PATH");` before starting the subprocess. – VGR Jun 07 '22 at 02:26
  • @VGR The linked question seems to want to do something like `System.getenv().remove("...")` from within the child process, after said process has been started. Though I feel like the `ProcessBuilder` approach should solve the OP's problem. – Slaw Jun 07 '22 at 02:40

1 Answers1

0

You can invoke the dynamic linker directly and ask it to preload certain libraries for your executable:

/lib64/ld-linux-x86-64.so.2 --preload /lib/x86_64-linux-gnu/libc.so.6 /bin/ls
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • Thank you. My version of `/lib64/ld-linux-x86-64.so.2` looks like it does not have `preload` only `--library-path`: `/lib64/ld-linux-x86-64.so.2 --preload /my/gtk/lib/` leads to `--preload: error while loading shared libraries: --preload: cannot open shared object file`. Also for `--library-path` I get: `Error: could not find libjava.so. Error: Could not find Java SE Runtime Environment.` – vesii Jun 08 '22 at 07:32