Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony JAVA_HOME not set

When trying to run signal-cli via Symfony it errors:

JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the location of your Java installation.

Output of echo $JAVA_HOME in the working directory:

/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-0.fc31.x86_64/jre

Output of java -version in the working directory:

openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-b09)
OpenJDK 64-bit Server VM (build 25.252-b09, mixed mode)

I'm trying to figure out how to resolve the error. Here is my code:

public function list()
{
  $list = new Process(['.././signal-cli-0.6.8/bin/signal-cli']);
  $list->run();

   if (!$list->isSuccessful()) {
       throw new ProcessFailedException($list);
    }
    echo $list->getOutput();
 }
}
like image 263
PopSmith Avatar asked Feb 13 '26 12:02

PopSmith


2 Answers

From docs (emphasis mine):

In addition to the env vars passed explicitly, processes inherit all the env vars defined in your system. You can prevent this by setting to false the env vars you want to remove [...]

source: https://symfony.com/doc/current/components/process.html#setting-environment-variables-for-processes

I believe that means that your user's/shell's env vars don't matter in that moment and you might have to set those explicitly:

 $process = new Process(['...'], null, ['ENV_VAR_NAME' => 'value']);

anyhow, you could write a small script that just writes the result of env/printenv into a file for further inspection. There's most likely a big difference, due to your shell running lots of additional scripts that impact environment that are not forwarded nor run for your process.

So you might have to explicitly pass JAVA_HOME to the process as implied above (this is a very specific home path, ):

$process = new Process(['...'], null, [
     'JAVA_HOME' => '/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-0.fc31.x86_64/jre', 
]);
like image 114
Jakumi Avatar answered Feb 16 '26 00:02

Jakumi


it seems that the process that executes your php application is executed as a user that doesn't contain the JAVA_HOME environment variable.

I mean when you execute the following command in your linux CLI :

echo $JAVA_HOME

The result is good because the environment variable is set for your linux user.

But when you call signal-cli via Symfony, it's not your linux user that is used, but the user of the process that executes your web server.

Maybe you use apache as a web server, then the user is something like "apache" or "www-data".

Then still in the case of apache, you'll have to pass the environment variable to the virtual host :

<VirtualHost yourhostname:80>
   ...
   SetEnv VARIABLE_NAME variable_value
   ...
</VirtualHost>

Or you can do as specified here

In case of other web servers, the solution will be a little different but the concept will be the same.

like image 29
Nico Avatar answered Feb 16 '26 01:02

Nico



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!