Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2002-06-04
5
Medium Priority
?
333 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

916 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