0

我遇到了一个与 PHP shell_exec() 相关的非常不寻常的问题。好吧,我实际上要执行外部java程序。我做这样的测试

<?php
    $command = 'C:\\Program Files\\Java\\jdk1.6.0_35\\bin\\java.exe';
    $val = shell_exec($command);

    echo('command:' . $command);
    echo('<BR>');
    echo('val:' . $val);
?>

一切都很好,但是当我尝试这样做时

<?php
    $command = 'C:\\Program Files\\Java\\jdk1.6.0_35\\bin\\javac.exe';
    $val = shell_exec($command);

    echo('command:' . $command);
    echo('<BR>');
    echo('val:' . $val);
?>

没有产出。真的很奇怪。我也尝试过使用 exec() 但没有什么不同。下一件奇怪的事情是当我尝试这个时

<?php
    $command = 'C:\\Program Files\\Java\\jdk1.6.0_35\\bin\\java.exe -version';
    $val = shell_exec($command);

    echo('command:' . $command);
    echo('<BR>');
    echo('val:' . $val);
?>

我使用确切的 java.exe,但添加了一个 -version 作为额外选项。没有输出出现。

java.exe 和 javac.exe 在命令行中执行时都会给出输出。我使用 Win 7 64 位、XAMPP 1.8.1(Apache 2.4.3、PHP 5.4.7)和 JDK 1.6 更新 35。

我在这里搜索了这个东西,并试图实现对相关问题的回答,但他们没有解决。

与此相关的任何解释,.? 感谢您的帮助 :)

4

1 回答 1

1

i searched an found the answer like this:

  1. java treat java.exe execution as a normal output when javac.exe as an error. this make the first code returns output but not with the second.
  2. the third code seems (un)like the first. yes it executes the java.exe, but with an aditional option -version. and java treat the output as an error. i have no idea why do they treat them differently.

so the code would be okay if we put and extra 2>&1 which will redirect the standard error to standard output.

<?php
    $command = '"C:\\Program Files\\Java\\jdk1.6.0_35\\bin\\javac.exe" 2>&1';
    $val = shell_exec($command);

    echo('command:' . $command);
    echo('<BR>');
    echo('val:' . $val);
?>

and so with this

<?php
    $command = '"C:\\Program Files\\Java\\jdk1.6.0_35\\bin\\java.exe" -version 2>&1';
    $val = shell_exec($command);

    echo('command:' . $command);
    echo('<BR>');
    echo('val:' . $val);
?>
于 2013-01-25T10:17:20.053 回答