Solved

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

Posted on 2002-06-07
3
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
This video teaches viewers about errors in exception handling.

690 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