• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Compile Java prg within Java prg, show errors

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");}
}

 
Serge Plourde
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
I am going to try this as soon as possible.
 
Serge Plourde
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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));
}
}
}
}
 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic