Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Opening PDF on button click and fill new document 2 53
MySqlDump not dumping triggers 1 52
ejb on wildfly 5 45
Java program running SQL query 5 46
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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:

685 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