简体   繁体   English

奇怪的PHP shell_exec Java行为

[英]weird PHP shell_exec Java behavior

i am having a very unusual problem related to PHP shell_exec(). 我有一个与PHP shell_exec()有关的非常不寻常的问题。 well, i am actually going to execute external java program. 好吧,我实际上将要执行外部Java程序。 i make a test like this 我做这样的测试

<?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);
?>

everything is OK, but when i am trying to do this 一切都很好,但是当我尝试这样做时

<?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);
?>

no output produced. 没有输出。 really odd. 真的很奇怪 i have also tried to use exec() but no different. 我也尝试过使用exec()但没有什么不同。 the next odd thing is when i am try this one 下一个奇怪的事情是当我尝试这个

<?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);
?>

i use the exact java.exe but add a -version as an extra option. 我使用确切的java.exe,但添加了-version作为额外选项。 no output come up. 没有输出。

either java.exe and javac.exe give output when they are executed in the command line. 在命令行中执行java.exe和javac.exe时都会输出。 i use Win 7 64bit, XAMPP 1.8.1 (Apache 2.4.3, PHP 5.4.7) and JDK 1.6 update 35. 我使用Win 7 64位,XAMPP 1.8.1(Apache 2.4.3,PHP 5.4.7)和JDK 1.6更新35。

i searched about this thing here, and tried to implement answer given to the related question, but they don't solve. 我在这里搜索了此问题,并试图实现对相关问题的回答,但他们没有解决。

any explanation related to this,.? 任何与此有关的解释。 thank you for helping :) 感谢您的帮助 :)

i searched an found the answer like this: 我搜索找到这样的答案:

  1. java treat java.exe execution as a normal output when javac.exe as an error. 当javac.exe错误时,java会将java.exe执行视为正常输出。 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. 是的,它执行java.exe,但带有附加选项-version。 and java treat the output as an error. 和Java将输出视为错误。 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. 因此,如果我们加上额外的2>&1,该代码就可以了,它将标准错误重定向到标准输出。

<?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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM