1 Reply Latest reply: Apr 20, 2012 6:38 PM by EJP RSS

    Running the .class file from java code

      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;

      try {

      while ((line = commandOutput.readLine()) != null) {
      System.out.println("command output: " + line);

      }//end while


      } catch (IOException e) {
      //log and/or handle it
      }//end catc

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