This discussion is archived
1 Reply Latest reply: Apr 20, 2012 4:38 PM by EJP RSS

Running the .class file from java code

932152 Newbie
Currently Being Moderated
I'm doing a kind of providing service like compiling and running Java code on server side and giving output to the end user.

Please suggest me an approach with code to run .class file from the Java code.

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));



String line = null;

System.out.print(commandOutput);
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
}//end catc

} catch (IOException e) {
System.err.println("IOException raised: " + e.getMessage());
}

}

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points