java.lang.Runtime
Hi all,
cud anyone help me with java.lang.Runtime. I am writing code to compile and execute another java class. The code is,
import java.util.*;
import java.io.*;
public class NewClass
{
public static void main(String args[])
{
try{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("javac C:\\HelloWorld.java");
StreamGobbler ig = new StreamGobbler(proc.getInputStream(), "output");
ig.start();
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
proc = rt.exec("java C:\\HelloWorld");
ig = new StreamGobbler(proc.getInputStream(),"printed version");
StreamGobbler igErr = new StreamGobbler(proc.getErrorStream(),"Error");
ig.start();
igErr.start();
exitVal = proc.waitFor();
System.out.println("ExitValue at the end"+exitVal);
}catch(Exception e){}
}
}
class StreamGobbler extends Thread
{
InputStream is;
String type;
StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
the class compiles fine, but when I execute it, following error comes
java.lang.NoClassDefFoundError: C:\HelloWorld
Exception in thread "main"
Could anyone help me with this. I wud appreciate your help.
regards
Ghani.
|