prain
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\\b in\\javac. exe Testme.java");
}
catch (Exception e) {System.out.println("Excep tion");}
}
}
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\\b
}
catch (Exception e) {System.out.println("Excep
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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\\b in\\javac. exe Top.java");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInp utStream() ));
String buff = null;
while ((buff = br.readLine()) != null) System.out.println(buff);
br.close();
} catch (IOException ioe) {
System.out.println("Except ion caught printing javac result");
ioe.printStackTrace();
}
}
catch (Exception e) {System.out.println("Excep tion");}
}
}
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\\b
try {
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInp
String buff = null;
while ((buff = br.readLine()) != null) System.out.println(buff);
br.close();
} catch (IOException ioe) {
System.out.println("Except
ioe.printStackTrace();
}
}
catch (Exception e) {System.out.println("Excep
}
}
is anything beeing written to System.out at all?
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.
Thx.
ASKER