Solved

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

Posted on 2002-06-04
5
322 Views
Last Modified: 2013-11-23
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");}
  }
}
0
Comment
Question by:prain
  • 3
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
pellep earned 100 total points
ID: 7054231
well, javac typically writes the result to the standard output stream of it's process. Try this for kicks

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   Testme.java");
   try {
   BufferdReader 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
 

Author Comment

by:prain
ID: 7054300
Thanks. Got it!
0
 

Author Comment

by:prain
ID: 7054402
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");}
}
}
0
 
LVL 4

Expert Comment

by:pellep
ID: 7055710
is anything beeing written to System.out at all?
0
 

Author Comment

by:prain
ID: 7055998
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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 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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

762 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

19 Experts available now in Live!

Get 1:1 Help Now