This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Compile Java prg within Java prg, show errors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Compile Java prg within Java prg, show errors" Watch "Compile Java prg within Java prg, show errors" New topic
Author

Compile Java prg within Java prg, show errors

Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
Has any one ever tried to compile a Java source code file from
within a Java program?
I was able to make it compile, but never able to get a hook on
the display of compilation errors!
Anyone got better results?
Thanks.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
I did a little more research and found that you can get the output from the Program run by the exec method using the getInputStream method of java.lang.Process. The new runtest.java is as follows
import java.io.*;
public class runtest
{
public static void main(String [] qq)
{
System.out.println("in the main");
try{
Runtime r;
r = Runtime.getRuntime();
Process p = r.exec("javac run.java");
BufferedReader reader = new BufferedReader(new InputStreamReader(new DataInputStream( new
BufferedInputStream(p.getInputStream()))));
String lineRead =null;
while( (lineRead = reader.readLine() ) != null) {
System.out.println("Input :"+ lineRead+ "\n");
}
}catch(Exception e){System.out.println("caught");}
System.out.println("out of the main");}
}


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
Thanks.
I am going to try this as soon as possible.
Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
It did not work!
From my command line:
D:\Serge\Langages\Java2\JavaEdit>java RunTest
in the main
out of the main
My RunTest was trying to compile a program that I purposefuly
wrote with errors.
I did not get any of the compilation errors.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Originally posted by Carl Trusiak:
I did a little more research and found that you can get the output from the Program run by the exec method using the getInputStream method of java.lang.Process. The new runtest.java is as follows
import java.io.*;
public class runtest
{
public static void main(String [] qq)
{
System.out.println("in the main");
try{
Runtime r;
r = Runtime.getRuntime();
Process p = r.exec("javac run.java");
BufferedReader reader = new BufferedReader(new InputStreamReader(new DataInputStream( new
BufferedInputStream(p.getInputStream()))));
String lineRead =null;
while( (lineRead = reader.readLine() ) != null) {
System.out.println("Input :"+ lineRead+ "\n");
}
}catch(Exception e){System.out.println("caught");}
System.out.println("out of the main");}
}

The above program works with a few other program types but, for some reason dosn't work with javac. I tried different types of input streams but, to no avail. Any thoughts?
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
My own thoughts, javac doen't write to the output stream, it writes to the err stream of the process. Need to find a way to capture the err stream from this runtime object.
Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
Could it be that we have to use System.err as input from the
exec process, or use its System.setErr to send the output to
a file.
Then we could display the content of the file, if compilation
errors were encountered.
I'll work on that too.
Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
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));
}
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compile Java prg within Java prg, show errors