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?

5
  • 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. Commented Jul 9, 2013 at 20:10
  • You can find the absolute path from the command line by typing which java. Commented Jul 9, 2013 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. Commented Jul 9, 2013 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. Commented Jul 10, 2013 at 2: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 Commented Jul 10, 2013 at 13:00

3 Answers 3

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");
Sign up to request clarification or add additional context in comments.

Comments

0

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

It's exec() not shell_exec()

More detail here

2 Comments

exec('java -version', $output) returns an empty array
Have you tried this too? <?php echo exec('java -version'); ?>
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.