2

I've read through a lot of similar SO questions about my issue, none seem to have my issue. If it's relevant, I'm running PHP 5.3.8 on Apache 2.2 and PHP exec() runs as nt authority\system

$cmd = "java -version";
$res = exec($cmd, $output, $return);

var_dump($res, $output, $return);

Produces:

string '' (length=0)

array
    empty

int 0

$return being 0 hints that this was successful. Interestingly, if I run:

$cmd = "java -version 2> response";

A file is created with the expected output:

java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode)

So my question: Why is $output not getting populated?

Comment update

Result of $res = exec("dir c:", $output, $return);

string '               2 Dir(s)  335,636,791,296 bytes free' (length=51)

array
    0 => string ' Volume in drive C is Local Disk' (length=32)
    1 => string ' Volume Serial Number is D87C-E25C' (length=34)
    2 => string '' (length=0)
    ...
    12 => string '               5 File(s)          1,158 bytes' (length=45)
    13 => string '               2 Dir(s)  335,636,791,296 bytes free' (length=51)

int 0
1
  • please check my updated answer, hope it will fix it for you Commented Sep 4, 2012 at 11:17

3 Answers 3

8

try this

$output = `java -version 2>&1`

watch for the backticks around the command, it serves exactly as shell command

2>&1 should redirect error to stdout since java by default use stderror !

Sign up to request clarification or add additional context in comments.

3 Comments

Tried that $output is just null
can you try something like `dir c:`
edited main question with output - all good - no issues with execution
0

You seem to compare apples with oranges as in second example you redirect stderr stream while you do not do that in first case. I suggest to always redirect stderr to stdout while using calls like exec otherwise you will lose this output

1 Comment

But why is output going to stderr? According to PHP the command executed successfully - note $return = 0
0

While not an "answer" - what I ended up doing was to redirect all output to STDOUT and check $return for 0.

This may be related to this bug, even though I'm running 5.3 it seems to match my scenario

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.