?
Solved

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

Posted on 2002-06-07
3
Medium Priority
?
224 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 200 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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 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 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:
Suggested Courses
Course of the Month9 days, 4 hours left to enroll

764 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