Solved

Re-Posting a modified question on java's Runtime Class

Posted on 2002-06-07
3
213 Views
Last Modified: 2013-11-23
I am writing a small example to see how the javac and java commands could be implemented using the Java's Runtime class. My intension is to build a small customed IDE to compile and run Java programs. The original Message is found here...
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=java&qid=20307704

Some one on this board suggested the following code. The file named Top.java is the file I just want to see get compiled when the Runtime invokes the javac compiler. When I run this program, it gets compiled. I see the a .class file is getting created. But for testing purposes I purposely made a syntax error in the Top.java class and recompiled. It did not do anything. I expected the errors to be listed on the DOS shell window. I never happened. I tried may other ways. I simply cannot see the errors listing, so that I could capture them and display in a GUI interface. Could someone throw some light?
Here is the code...

class Top
{
  public void methodwitherrors()
  {
   System.out.prin();
  }
}

import java.io.*;
public class RuntimeTest
{

public static void main(String args[])
{
try{
Runtime rt = Runtime.getRuntime();

 Process p = rt.exec("d:\\j2sdk1.4.0\\bin\\javac.exe   Top.java");
 try {
 BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
 String buff = null;
 while ((buff = br.readLine()) != null) System.out.println(buff);
 br.close();
 } catch (IOException ioe) {
 System.out.println("Exception caught printing javac result");
 ioe.printStackTrace();
 }

}
catch (Exception e) {System.out.println("Exception");}
}
}
0
Comment
Question by:prain
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
Ovi earned 50 total points
ID: 7062259
The ideea is to read the Error stream too. There you'll get your errors. Works fine on my computer, you just need to readjust the path to javac.

import java.io.*;

public class RuntimeTest {

  public static void main(String args[]) {
    try{
      Runtime rt = Runtime.getRuntime();

      final Process p = rt.exec("/usr/lib/jdk1.4.0/bin/javac Top.java");
        new Thread(new Runnable() {
       public void run() {
            try {
              BufferedReader br_in = new BufferedReader(new InputStreamReader(p.getInputStream()));
              String buff = null;
              while ((buff = br_in.readLine()) != null) {
             System.out.println("Process out :" + buff);
             try {Thread.sleep(100); } catch(Exception e) {}
           }
              br_in.close();
            } catch (IOException ioe) {
              System.out.println("Exception caught printing javac result");
              ioe.printStackTrace();
            }
       }
     }).start();

        new Thread(new Runnable() {
       public void run() {
            try {
              BufferedReader br_err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
              String buff = null;
              while ((buff = br_err.readLine()) != null) {
                   System.out.println("Process err :" + buff);
             try {Thread.sleep(100); } catch(Exception e) {}
           }
              br_err.close();
            } catch (IOException ioe) {
              System.out.println("Exception caught printing javac result");
              ioe.printStackTrace();
            }
       }
     }).start();
    } catch (Exception e) {
      System.out.println("Exception");
      e.printStackTrace();
    }
  }
}
0
 
LVL 9

Expert Comment

by:Ovi
ID: 7106572
What are you doing prain ?
0
 

Author Comment

by:prain
ID: 7107090
Sorry, was late.
Good Ans.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now