Java 1.6 Broken when called by background Symfony

2019-02-04 02:30发布

问题:

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.

回答1:

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.



回答2:

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


回答3:

This worked for me:

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

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