Got it!
If there is any error in the compilation, it will be output to
the System.out.err. The program must catch it using the Process.
getErrorStream() method.
If there is a better, simpler way to do this, please let me know.
Here is the
test program:
/*
Run it with:
Java TestRun <nameOfYourJavaSourceCodeFileToCompile>
without the ".java" extension.
*/
public class TestRun
{
public static void main (String [] args)
throws IOException, InterruptedException
{
Runtime r = Runtime.getRuntime();
Process p = r.exec("javac " + args[0] + ".java");
//All compilation errors will go to the System.out.err //stream, so get it!
InputStreamReader in =
new nputStreamReader( p.getErrorStream() );
BufferedReader br = new BufferedReader( in );
String inputLine;
Vector vLines = new Vector();
while ( (inputLine = br.readLine() ) != null)
{
vLines.addElement(inputLine);
}
System.out.println("Compilation of " + args[0] + ".java:");
if ( ( p.waitFor() == 0 ) )
{
System.out.println( "Terminated normally." );
}
else
{
System.out.println( "Terminated abnormally." );
for (int i=0; i < vLines.size(); ++i)
{
System.out.println((String) vLines.elementAt(i));
}
}
}
}