Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2002-06-04
5
Medium Priority
?
330 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
[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
  • 3
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
pellep earned 400 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …
Suggested Courses

688 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