3

I am having troubles with the passthru() command when executing Java programs. More specifically with setting the LD_LIBRARY_PATH using passthru.

I tried:

  • setting the .so file path directly like this:

passthru("java -Djava.library.path='.:/path/to/directory/of/.so/object' " HelloWorld);

  • writing a shell script which sets the LD_LIBRARY_PATH,then executing the shell script using passthru.

  • acessing the /etc/profile file and adding the "export LD_LIBRARY_PATH..." line in the document.

These are just few solutions I found online which I thought would work, but none worked. I believed the problems come from concatenation stuff in the passthru command as I saw on several sites stuff like that:

$command = 'export LD_LIBRARY_PATH="' . $path_to_library_dir .'"; ' . $path_to_binary; passthru($command);

which I also tried.But I dont know what am doing wrong.

Please, can anyone help?

Thank you!


EDIT 1

Using @lexmihaylov suggestion:

Hi, Thank you for replying. Actually I get an "java.lang.UnsatisfiedLinkError:/usr/lib/libjpcap.so". So that's why I am trying to set up the LD_LIBRARY_PATH to my .so object.

From your answer I tried:

$Path_to_library_dir = '/usr/lib64/jpcap';
$Path_to_binary = '/usr/lib64/jpcap/libjpcap.so'; 
$command = 'LD_LIBRARY_PATH="' . $Path_to_library_dir .'" '.$Path_to_binary; 

passthru($command);

$java_execution = "java myjavaprogram 2>&1"; 
echo passthru($java_execution,$output);
    echo $output;

I still get the same java.lang.UnsatisfiedLinkError. But if I set the LD_LIBRARY_PATH directly in my terminal, my java program works fine when executed via terminal.

Thank you for helping!

1 Answers1

0

It should work with you second example but remove the export and ';' from the shell command. Just try this and tell me what happens:

$command = 'LD_LIBRARY_PATH="' . $path_to_library_dir .'" ' . $path_to_binary;
passthru($command);
lexmihaylov
  • 717
  • 3
  • 8
  • Try replacing passthru with system and use putenv() for setting LD_LIBRARY_PATH – lexmihaylov May 30 '13 at 19:56
  • no I tried with system and it still returns me the same error. Any other idea please? – user1839550 May 30 '13 at 19:59
  • can you give me the command lines that you are executing from shell? – lexmihaylov May 30 '13 at 20:07
  • From the terminal I directly do: > `export LD_LIBRARY_PATH="/usr/lib64/jpcap/:$LD_LIBRARY_PATH"` > `java myjavaprogram` – user1839550 May 30 '13 at 20:16
  • Ok .. the way that it has to be done with my example is that all 2 lines should be one command. So - passthru('LD_LIBRARY_PATH="/usr/lib64/jpcap/:$LD_LIBRARY_PATH" java ./myjavaprogram');. Does that make sense and if you tried it already - please excuse my stupid comment :) – lexmihaylov May 30 '13 at 20:22
  • There is no stupid comment as I was not able to find any solution for my problem. So anything to tryout is welcomed.:) Unfortunately it still does not work but can you please explain why you placed the **"./"** before the **myjavaprogram?** – user1839550 May 30 '13 at 20:35
  • "./" is just a habit that i have for executing scripts from the current folder. I'm really disappointed that nothing worked. The last thing that i can think of is adding the export LD_LIBRARY_PATH="/usr/lib64/jpcap/:$LD_LIBRARY_PATH" to .bashrc file in your user's home directory – lexmihaylov May 30 '13 at 20:40
  • I added it to my .bashrc file and I know that it works as I no longer have to do "export LD_LIBRARY_PATH..." in my terminal when I run my javaprogram. But when running it with php with system/passthru/shell_exec...it still does not work. I have one thing that I don't know might provide with more info, when I do shell_exec(java -version), nothing is returned. Whereas when I do it in terminal it works. Is it related to my problem?:S – user1839550 May 31 '13 at 05:08