capturing java compile errors with php

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
cr1ms0n3cho
Forum Newbie
Posts: 3
Joined: Sun Feb 15, 2009 6:02 pm

capturing java compile errors with php

Post 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 :(
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: capturing java compile errors with php

Post 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
cr1ms0n3cho
Forum Newbie
Posts: 3
Joined: Sun Feb 15, 2009 6:02 pm

Re: capturing java compile errors with php

Post 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!
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: capturing java compile errors with php

Post 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.
cr1ms0n3cho
Forum Newbie
Posts: 3
Joined: Sun Feb 15, 2009 6:02 pm

Re: capturing java compile errors with php

Post by cr1ms0n3cho »

its on my schools web server.
I believe they are using openSUSE but I'm not positive. It's definitely not windows.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: capturing java compile errors with php

Post by Jenk »

Code: Select all

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