How can I run the compiled code (.class) java from the java code itself?
I'm doing a kind of providing service like compiling and running java code on server side and giving output to the end user.
Can anyone suggest an approach that will accomplish this?
import java.io.*;
public class demo {
public static void main(String args[]) throws IOException, InterruptedException {
int result;
try {
System.out.println("command output:");
Process proc = Runtime.getRuntime().exec("java -cp . demoh");
InputStream in = proc.getInputStream();
result = proc.waitFor();
BufferedInputStream buffer = new BufferedInputStream(proc.getInputStream());
BufferedReader commandOutput = new BufferedReader(new InputStreamReader(buffer));
System.out.print(commandOutput);
String line = null;
try {
while ((line = commandOutput.readLine()) != null) {
System.out.print(line);
System.out.println("command output: " + line);
}//end while
commandOutput.close();
} catch (IOException e) {
//log and/or handle it
}
} catch (IOException e) {
System.err.println("IOException raised: " + e.getMessage());
}
}
}
OutOfMemroyError,StackOverflowError, endless loops..). In fact, once I had an on-line compiler (no option to run the code). A person who had deep knowledge of the compiler I was using, got in contact and warned me of DOS attacks that were purely based on compiling code. There was (at least long ago) code that could tie up the compiler for 30 minutes or more!