28

I have a Symfony task that generates some files calls exec to a jar and then parses the output. The jar runs fine from the command line, the task runs fine from the command line.

The problem:

I call the task in an action based on a form submission. I have the action start a new php process in the background to run the task regardless of what the page the spawned it does now.

When it gets to the java call, say exec(java -version); it outputs this:

Error occurred during initialization of VM
Unable to load native library: libjava.jnilib

I feel like it has to do with the way I call php when I start the task but I'm lost as to why it wouldn't have the same libraries as when I use the command line.

How can I make java run from the 'background' Symfony task?

Notes:

It used to work with no hitch until I upraded mamp from 1.9.6 to 2.0.3.

I've looked at: Broken Java Mac 10.6 but since I can run it fine from the command line it seems to be a different issue.

I've also looked at Execute symfony task command from the shell_exec() permission denied but I don't think permissions are the issue here.

Update:

I've narrowed down the problem to MAMP and getting to php from the browser.

<?php
echo exec("java -version")
...

Will work when called from the command line but not when the php file is opened through the browser. So the way MAMP is configured is causing the issue.

Here's the environment info:

  • Variable Value
  • SHELL /bin/bash
  • TMPDIR /var/folders/YH/YH+uW3hDHZyxQ5AiUtr0T++++TI/-Tmp-/
  • Apple_PubSub_Socket_Render /tmp/launch-3rr9ZI/Render
  • USER myuser
  • COMMAND_MODE unix2003
  • SSH_AUTH_SOCK /tmp/launch-zinaMI/Listeners
  • __CF_USER_TEXT_ENCODING 0x1F5:0:0
  • PATH /usr/bin:/bin:/usr/sbin:/sbin
  • PWD /
  • HOME /Users/myuser
  • SHLVL 2
  • DYLD_LIBRARY_PATH /Applications/MAMP/Library/lib:
  • LOGNAME myuser
  • DISPLAY /tmp/launch-FYrw70/org.x:0
  • _ /Applications/MAMP/Library/bin/httpd

Dyld seems to be present in here. I need to find a way to unset it from mamp's environment.

Solved

I've figured out a solution. It seems like a hack but it worked. I'll post it here just incase anyone else runs into the same problem.

As Broken Java Mac 10.6 mentions the DYLD_LIBRARY_PATH must be unset. Not sure why, it seems to be needed on Unix systems but not MacOSX.

If MAMP sets to /Applications/MAMP/Library/lib here's how to disable it: Edit /Applications/MAMP/Library/bin/envvars and comment out the following lines

DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
export DYLD_LIBRARY_PATH

So that it looks like this:

#DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
#export DYLD_LIBRARY_PATH

This should fix the problem and java 1.6 can run fine.

Is this a hack? or is this a bug in MAMP? Please answer if you know a better way to solve this issue.

Community
  • 1
  • 1
paaat
  • 545
  • 5
  • 14
  • Re: "I've looked at: [Broken Java Mac 10.6](http://stackoverflow.com/questions/1482450/broken-java-mac-10-6) ..." --> Check your `phpinfo(INFO_ENVIRONMENT)` to check the environment of your PHP process. It might be different from the env of the command shell you used to manually check if you can launch Java. – Barend Oct 04 '11 at 15:20
  • Which part of the environment info am i looking at specifically?Java works fine called from php in the command line: `php -r 'exec("java -version", $o); echo $o;'` echoes: `java version "1.6.0_26"...` It's when I spawn the php process from php that it can't run java. I'll try to see if that path is different. – paaat Oct 04 '11 at 15:37
  • I've noticed something strange, the environement returns PWD as being symfonyroot/web but getcwd returns symfonyroot only. Not sure if this is significant. – paaat Oct 04 '11 at 16:09
  • Thanks for the hint turns out the answer was in `phpinfo(INFO_ENVIRONMENT)` after all but I had to run it through the browser to see it. – paaat Oct 04 '11 at 17:35
  • 5
    You should add the "Solved" section as an answer, wait 24 hours, and then accept your answer. You won't get any rep but the question will show up as answered. – Jim Garrison Feb 09 '12 at 22:43
  • 2
    Please post a solution below so we can remove this from the Unanswered list. Thanks. – Bill the Lizard Feb 14 '12 at 13:58

3 Answers3

12

This is the solution of @paaat added. Im just posting to get this question out of the unanswered list.

I've figured out a solution. It seems like a hack but it worked. I'll post it here just incase anyone else runs into the same problem.

As Broken Java Mac 10.6 mentions the DYLD_LIBRARY_PATH must be unset. Not sure why, it seems to be needed on Unix systems but not MacOSX.

If MAMP sets to /Applications/MAMP/Library/lib here's how to disable it: Edit /Applications/MAMP/Library/bin/envvars and comment out the following lines

DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
export DYLD_LIBRARY_PATH

So that it looks like this:

#DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
#export DYLD_LIBRARY_PATH

This should fix the problem and java 1.6 can run fine.

Be sure to restart your installation of MAMP for the changes to take effect.

Phil Rykoff
  • 11,999
  • 3
  • 39
  • 63
barsju
  • 4,408
  • 1
  • 19
  • 24
3

This worked! The version of MAMP I'm running, 2.1.3, however has a different contents in the file:

#if test "x$DYLD_LIBRARY_PATH" != "x" ; then
#  DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
#else
#  DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib"
#fi
#export DYLD_LIBRARY_PATH
Ben Sullins
  • 231
  • 2
  • 9
0

This worked for me:

<?php
  exec('export DYLD_LIBRARY_PATH=""; java -version');
?>

References:
- https://drupal.org/node/1257654
- Calling java from PHP exec

Community
  • 1
  • 1
Meetai.com
  • 6,622
  • 3
  • 31
  • 38
  • Please, don't go [posting the **same** Answer around](http://stackoverflow.com/search?q=user%3A1130803+DYLD_LIBRARY_PATH). That means that the Questions are duplicates, choose the best one to post your answer and flag the others as duplicates of that one. – brasofilo May 30 '14 at 09:25