Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP exec(";javac file.java>;>;log.txt";)不会将编译错误写入文件_Java_Php - Fatal编程技术网

PHP exec(";javac file.java>;>;log.txt";)不会将编译错误写入文件

PHP exec(";javac file.java>;>;log.txt";)不会将编译错误写入文件,java,php,Java,Php,我正在编写一个小php脚本,它使用exec函数编译java代码。我希望编译失败并将编译问题写入日志文件,但当我对其进行cat时,日志文件不包含任何内容。如何将编译错误消息获取到该日志文件中 <?php ini_set('display_startup_errors', 1); ini_set('display_errors', 'On'); error_reporting(E_ALL); // When it doesn't compile, it doesn't write

我正在编写一个小php脚本,它使用exec函数编译java代码。我希望编译失败并将编译问题写入日志文件,但当我对其进行cat时,日志文件不包含任何内容。如何将编译错误消息获取到该日志文件中

<?php 

 ini_set('display_startup_errors', 1);
 ini_set('display_errors', 'On');
 error_reporting(E_ALL);

// When it doesn't compile, it doesn't write error to log.txt 
$class = 'class runrunrun
    {
        public static void main (String args[])
        {
            System.out.println("Hello world!"); 
            FAIL NOW!!
        } 

    }'; 

    $myfile = fopen("runrunrun.java", "w+") or die("Unable to open file!");
    fwrite($myfile, $class);
    fclose($myfile);

    exec("javac runrunrun.java >> log.txt", $foo, $compileFlag);
        if ($compileFlag != 0) {
            echo "COMPILE ERROR\n";
        }

?>

您需要使用
2>
STDERR
定向到文件。您当前仅重定向
STDOUT

javac runrunrun.java >> log.txt 2>> error.log
我可以为你工作

或者,如另一条注释中所述,将所有输出重定向到带有
2>&1

javac runrunrun.java 2>&1 log.txt

另请参见。

不清楚您使用的是什么O/s和shell。如果您使用的是Mac或Unix设备,那么您可能正在使用Bash,在这种情况下,您需要执行“javac runrun.java 2>&1>>log.txt”。这会将stderr重定向到stdout。让我们更多地了解您的环境。@stdunbar感谢您指出这一点!我已根据您询问的信息更新了问题。我希望这有帮助