2

I am writing a small php script that compiles java code using the exec function. I want the compile to fail and write compile issues to a log file but the log file doesn't contain anything when I cat it. How can I get compile error messages into that log file?

<?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";
        }

?>

Thanks in advance

EDIT: I'm trying to run this on an AFS Server. Here's the output from lsb_release. I hope this clears it up a bit

LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch

Distributor ID: Scientific

Description: Scientific Linux release 6.8 (Carbon)

Release: 6.8

Codename: Carbon

The default shell is BASH

  • 3
    It's unclear what O/S and shell you're using. If you are on a Mac or Unix box, at a guess you're using Bash in which case you'll want to do "javac runrunrun.java 2>&1 >> log.txt". This redirects stderr to stdout. Let us know more about your environment. – stdunbar Mar 28 '17 at 01:55
  • @stdunbar Thank you for pointing that out! I've updated the question for the info you asked. I hope this helps –  Mar 28 '17 at 14:18

1 Answers1

0

You will want to direct STDERR to a file using 2>>. You are currently only redirecting STDOUT.

javac runrunrun.java >> log.txt 2>> error.log

May work for you.

Or, as mentioned in another comment, redirect all output to the same file with 2>&1

javac runrunrun.java 2>&1 log.txt

See also this answer.

Community
  • 1
  • 1
Mike
  • 1,968
  • 18
  • 35