Solved

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

Posted on 2002-06-04
5
327 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 30
servlet example 11 50
jdbc error in jsp application 20 64
how to see all occupied ports on windows 10 laptop 15 77
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

808 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