Page 1 of 1

capturing java compile errors with php

Posted: Sun Feb 15, 2009 6:12 pm
by cr1ms0n3cho
i am trying to write a php script similar to that found at http://www.javabat.com
and im having some problems hope you can help.

This is some sample java code from a text box:

Code: Select all

 
public static int timesTwo(int x){
    //should throw a return type error
    return;
}
 
And this is the php that i use to write the file.

Code: Select all

 
//SRC from post
$userCode = $_POST['src'];
//TODO: GET FROM SESSION
$user="admin";
$questionId=1;
//Unique file name guarentees file overwriting
//java classes have to start with a letter
$fileName =  "x".sha1($user.$questionId);
$fp = fopen($path.$fileName.".java","w");
//default class 
$src = "
//Genereted by runTest.php
public class $fileName{
    public static void main(String args[]){
        System.out.println(timesTwo(1));
    }
$userCode
}";
fwrite($fp,$src);
fclose($fp);
/*
    Compiles and runs the code then echos the output
    for user feedback
    
    TODO: 
        Infinite Loop Checks
        Compilation Error Checks
        Sanatize inputs!!!!!
*/
echo "Compiling... \n";
//command for compiling a text file
echo system("javac ".$path.$fileName."java",$compileOutput);.
 
if there are no compilation errors the file gets written to my web server as expected but i cannot capture any errors if they exist :(

Re: capturing java compile errors with php

Posted: Wed Feb 18, 2009 9:18 am
by mintedjo
Not sure how to make compile errors print to stdout but you can direct them to a file and then check if the file has anything in it.
If the file has any contents then there were errors and you can show them to the user.

Code: Select all

javac -Xstdout error.log MyJava.java
Thats what I would do because I dont know any other way :-P

Re: capturing java compile errors with php

Posted: Wed Feb 18, 2009 9:38 am
by cr1ms0n3cho
i figured it out yesterday :)
if you do

Code: Select all

javac mysrc.java 2 > output.text
it will take stderr and write it to a file. Esscentially the same thing

Thanks thought!

Re: capturing java compile errors with php

Posted: Wed Feb 18, 2009 9:43 am
by mintedjo
HI again :-)
That was my first thought and it didnt work for me on windows.
Did you have to change any javac settings or anything?
Or are you using linux perhaps? I'm just wondering in case I ever need to do this in the future.

Re: capturing java compile errors with php

Posted: Wed Feb 18, 2009 9:49 am
by cr1ms0n3cho
its on my schools web server.
I believe they are using openSUSE but I'm not positive. It's definitely not windows.

Re: capturing java compile errors with php

Posted: Fri Feb 20, 2009 12:00 pm
by Jenk

Code: Select all

$return = shell_exec("javac mysrc.java 2>&1");