cloakedninjas
cloakedninjas

Reputation: 4186

PHP exec Java on Windows produces no output

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

Upvotes: 2

Views: 2030

Answers (3)

cloakedninjas
cloakedninjas

Reputation: 4186

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

Upvotes: 0

Hawili
Hawili

Reputation: 1659

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 !

Upvotes: 8

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

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

Upvotes: 0

Related Questions