-1

Possible Duplicate:
How to Run Java Source Code within a Java Program

Our group wants to run a java source code inside a java program/application, given that the syntax inside is error free. how can this be? would we still need to compile for errors? or is compiling inescapable? Thank you ...

Just like netbeans can run its codes underneath.

Community
  • 1
  • 1

2 Answers2

0

You can use this function java.lang.Runtime.exec(), link, and here is another one on how to do it

NightWhisper
  • 510
  • 1
  • 4
  • 12
0

Here is how to run a java (or another external) program from java code using the Runtime exec method, and how to read command's output and possible errors during execution:

import java.io.*;

public class JavaRunCommand {

    public static void main(String args[]) {

        String s = null;

        try {        
            // run the Unix "ps -ef" command
            // using the Runtime exec method:
            Process p = Runtime.getRuntime().exec("ps -ef");

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

            // read the output from the command
            System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }

            // read any errors from the attempted command
            System.out.println("Here is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }

            System.exit(0);
        }
        catch (IOException e) {
            System.out.println("exception happened - here's what I know: ");
            e.printStackTrace();
            System.exit(-1);
        }
    }
}

More details: http://alvinalexander.com/java/edu/pj/pj010016

arutaku
  • 5,937
  • 1
  • 24
  • 38