Link to home
Start Free TrialLog in
Avatar of prain
prainFlag for United States of America

asked on

How to invoke the Javac and Java commands from an interface?

Hi,

I am in the process of building a IDE for complilation,
and invoking a java program. I know there are many IDE's available to do this. So one should not ask me the question... "Why you want to do that? Use one already developed.

I am doing this for learning porposes as well. How would I invoke the javac command on the click of a button on a GUI?

I tried the Runtime class' exec() method. But that does not do anything. It executes the line but nothing happens.
Here is my code.....

If the following example works, it should also work when clicking a button. This one does not work. So, can anyone throw some light into this?

public class RuntimeTest
{

  public static void main(String args[])
  {
   try{
   Runtime rt = Runtime.getRuntime();
   
    rt.exec("d:\\j2sdk1.4.0\\bin\\javac.exe   Testme.java");
 
  }
   catch (Exception e) {System.out.println("Exception");}
  }
}
ASKER CERTIFIED SOLUTION
Avatar of pellep
pellep
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prain

ASKER

Thanks. Got it!
Avatar of prain

ASKER

But another part to this....

Yes I can see it compiles and create a new class files. However I notice that if there is a systax error in the file that is being compiled (Top.java - see below), the errors are not being displayed on the screen. how to capture the errors?

Here is the corrected code. there were few syntax errors in the code you modified.

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");}
}
}
is anything beeing written to System.out at all?
Avatar of prain

ASKER

NO. Nothing. Just I see the new .class file is created if there are no errors. If there are errors, even the old .class files would not get deleted. I checked the time created. I made an error purposely in the Top.java file to test this... to see what happens when there is s syntax error.

Thx.