2

I'm using the command:

shell_exec("java -version");

to detect what Java version is installed. Java IS installed. The PHP script runs under the user "daemon". Running this command from the command line:

su daemon -c 'java -version'

outputs

java version "1.6.0_27"
OpenJDK Runtime Environment (IcedTea6 1.12.1) (6b27-1.12.1-2ubuntu0.12.04.2)
OpenJDK Client VM (build 20.0-b12, mixed mode, sharing)

I know using shell_exec works with Java and PHP because elsewhere in the code I'm running java .jar files using it.

Am I missing something here?

swl1020
  • 816
  • 14
  • 34
  • Most likely the process running the php script does have a different `PATH` defined. That variable is typically defined in a shell startup script, that might make a difference. Have a try to address the java executable using an absolute path. – arkascha Jul 09 '13 at 20:10
  • You can find the absolute path from the command line by typing `which java`. – nickb Jul 09 '13 at 20:11
  • Yeah, I agree the path is the likely culprit. Also, consider other environment variables, such as HOME - I've previously [encountered that problem](http://stackoverflow.com/questions/9967217/inkscape-inside-php-apache-doesnt-render-fonts-to-png). – halfer Jul 09 '13 at 20:20
  • I would argue, as per my answer below, that PATH and other environment variables are not the issue due to the shell behavior using backticks, which exec_shell emulates. – Myles Jul 10 '13 at 02:12
  • @arkascha - But elsewhere in the code I'm running "java file.jar" and it runs the file.jar just fine. Also, I compared the "which java" from the command line as root, and from a print statement in the code, and they are the same: /usr/bin/java – swl1020 Jul 10 '13 at 13:00

3 Answers3

3

Add 2>&1 to the end of your shell command to have STDERR returned as well as STDOUT.

$output = shell_exec("java -version 2>&1");
Lord_Dracon
  • 290
  • 1
  • 4
  • 14
0

Try this - exec('java -version', $output);

It's exec() not shell_exec()

More detail here

SSaikia_JtheRocker
  • 5,053
  • 1
  • 22
  • 41
0

Looks like Java is sending the output to stdout directly. If you run the command with backticks on the command line (as the documentation says the command is equivalent to), and try to store it in a variable, you'll see it gets printed out, but not stored in the variable.

For example:

foo=`java -version`
print $foo // results in nothing

However:

foo=`ls`
print $foo // results in the results of ls

You might try using exec with the output variable instead.

Myles
  • 20,860
  • 4
  • 28
  • 37