Solved

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

Posted on 2002-06-07
3
211 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

705 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

18 Experts available now in Live!

Get 1:1 Help Now