Using exec() to run a class
Hi,
I would like to run a class file from my application. I managed to compile the java file, but got stack with executing it. To be more clear, i will post my code here:
Code:
public static void main(String args[]) throws Exception
{
try {
String line;
Runtime rt = Runtime.getRuntime();
String[] cmd = {
"c:/progra~1/Java/jdk1.5.0_05/bin/javac.exe", "c:/Temp/Test1/MainTest.java"};
Process p = rt.exec(cmd);
int exitVal = p.waitFor();
System.out.println("After compilation value: " + exitVal);
String[] cmd2 = {
"cmd.exe", "/c","c:/progra~1/Java/jdk1.5.0_05/bin/java.exe -cp MainTest"};
Runtime rt2 = Runtime.getRuntime();
p = rt2.exec(cmd2);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String temp = "";
while((temp = input.readLine()) != null) {
System.out.println(temp);
}
input.close();
}
catch(IOException e) {
System.err.println("Error on exec() method");
e.printStackTrace();
}
}
After compilation the exit value is 0 and creates a class file. But when i try to run it is either jumps to an exception and prints errors or prints the error from a BufferedReader:
java.lang.NoClassDefFoundError: c:/Tem/Test1/MainTest
Exception in thread "main"
I have tried several way for cmd2 commands but just would not run.
Any help would be much appriciated.