Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to set LD_LIBRARY_PATH for Java process

Tags:

java

linux

csh

dll

I am trying to call my linux executable from shell script. Before calling this executable, I want to set LD_LIBRARY_PATH with specific values. My shell script is as below:

Parent.sh (contains 2 lines)

   - source set_env.sh 
   - executable.so

Set_env.sh

   - setenv LD_LIBRARY_PATH /proj/something

On manually executing Parent.sh scipt from linux console, the executable.so is called with LD_LIBRARY_PATH set correctly. But after integrating it wiht java code as:

String[] commandArray ={"Parent.sh"};
Runtime runtime = Runtime.getRuntime();
Process javap = runtime.exec(commandArray);
javap.waitFor();

LD_LIBRARY_PATH is not set for executable.so

I hope description is clear :)

Please let know whats wrong in the code.

like image 536
Darshan Nair Avatar asked Jan 22 '12 10:01

Darshan Nair


People also ask

How do I set LD_LIBRARY_PATH?

In your terminal, type the following sudo ldconfig and press enter on your keyboard. Close all your open terminals that you were using then open a new terminal session and run echo $LD_LIBRARY_PATH If you see the path you added is echoed back, you did it right.

What is LD_LIBRARY_PATH Java?

The LD_LIBRARY_PATH environment variable tells Linux applications, such as the JVM, where to find shared libraries when they are located in a different directory from the directory that is specified in the header section of the program.

How do I change the library path in Linux?

How do I set the Library path under Linux operating systems? You need to use ldconfig config file and ldconfig command which creates the necessary links and cache to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld. so.

What is LD_LIBRARY_PATH variable?

LD_LIBRARY_PATH is an environmental variable used in Linux/UNIX Systems. It is used to tell dynamic link loaders where to look for shared libraries for specific applications. It is useful until you don't mess with it.


2 Answers

Dunes answer solves your problem, but I would strongly suggest a different approach in this particular case. Instead of relying on a shell to set the environment arguments, you should do this in your Java code. This way you don't need to know which shells exist on the system and what their language is, it will just work on all platforms.

To do this, you can use the Runtime.exec(String[] cmd, String[] environment) overload (javadoc). As the second parameter you can pass an array which contains all the environment variables the subprocess will see.

A little bit nicer even is the ProcessBuilder API:

ProcessBuilder pb = new ProcessBuilder("executable.so");
Map<String, String> env = pb.environment();
env.put("LD_LIBRARY_PATH", "/proj/something");
Process javap = pb.start();
javap.waitFor();

This way, the subprocess will inherit all environment variables from the Java process, and additionally have the LD_LIBRARY_PATH variable set.

like image 159
Philipp Wendler Avatar answered Oct 02 '22 19:10

Philipp Wendler


Are you sure the subprocess is using csh? If it starts up using bash or something else then this would prevent the script from working (but not to throw an IOException).

You should really aad a hashbang line as the very first line of your script to state which shell interpreter you wish to use.

eg.

#!/usr/bin/env csh
like image 24
Dunes Avatar answered Oct 02 '22 18:10

Dunes