-1

using the CompileAndRun class, i can now compile and run my HelloWorld class. now i want to use this to run a program that requires users input. this may either be a command line argument or input received through stdin.

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class CompileAndRun {

public static void main(String[] args) {
    new CompileAndRun();
}

public CompileAndRun() {
    try {
        int result = compile("compileandrun/HelloWorld.java");
        System.out.println("javac returned " + result);
        result = run("compileandrun.HelloWorld");
    } catch (IOException | InterruptedException ex) {
        ex.printStackTrace();
    }
}

public int run(String clazz) throws IOException, InterruptedException {        
    ProcessBuilder pb = new ProcessBuilder("java", clazz);
    pb.redirectError();
    pb.directory(new File("src"));
    Process p = pb.start();
    InputStreamConsumer consumer = new InputStreamConsumer(p.getInputStream());
    consumer.start();

    int result = p.waitFor();

    consumer.join();

    System.out.println(consumer.getOutput());

    return result;
}

public int compile(String file) throws IOException, InterruptedException {        
    ProcessBuilder pb = new ProcessBuilder("javac", file);
    pb.redirectError();
    pb.directory(new File("src"));
    Process p = pb.start();
    InputStreamConsumer consumer = new InputStreamConsumer(p.getInputStream());
    consumer.start();

    int result = p.waitFor();

    consumer.join();

    System.out.println(consumer.getOutput());

    return result;        
}

public class InputStreamConsumer extends Thread {

    private InputStream is;
    private IOException exp;
    private StringBuilder output;

    public InputStreamConsumer(InputStream is) {
        this.is = is;
    }

    @Override
    public void run() {
        int in = -1;
        output = new StringBuilder(64);
        try {
            while ((in = is.read()) != -1) {
                output.append((char) in);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            exp = ex;
        }
    }

    public StringBuilder getOutput() {
        return output;
    }

    public IOException getException() {
        return exp;
    }
}
}
  • 2
    You've described a problem, but have so far not asked a question (let alone a specific, answerable question). What *is* your question? – Andrew Thompson Jul 05 '13 at 15:13
  • It is not clear what the question is, hence requested to close. – Ingo Jul 05 '13 at 15:15
  • Note that the javax.tools package provides programmatic access to the compiler: http://docs.oracle.com/javase/7/docs/api/javax/tools/package-summary.html – Puce Jul 05 '13 at 15:29

3 Answers3

1

In my opinion better solution is use of the Java 6 Compiler API. You should look at javax.tools package documentation too.

zacheusz
  • 8,750
  • 3
  • 36
  • 60
0

You should use the Runtime class to do so.

code file --x.java to execute-y.java

Now you should be using the "exec" function of Runtime class.

like this

Process p=Runtime.getRuntime().exec("cmd /c javac y.java && java y.java");

so now the program executes from.a process made by this class.

This method returns a process

so in order to receive the output of this command just executed do like this

p.getOutputStream() // wrap this outputStream in a outputstream wtiter or may be a Printwriter and read it till its not null

and finally output it.

cafebabe1991
  • 4,928
  • 2
  • 34
  • 42
  • my question is- my other program say "HelloWorld" requires the user to enter his name through stdin and then prints hello "name". now i hv a created a gui, where when the user clicks on compile and run button, the CompilandRun class is executed. now when the CompileandRun program runs, it should ask the user to enter name in the gui, process that input and then print hello aaa – user2549648 Jul 06 '13 at 11:07
  • then you need to append the input received to the output of another program that says hello` – cafebabe1991 Jul 09 '13 at 06:25
0

You can add command line arguments by adding another string in ProcessBuilder constructor like ProcessBuilder pb = new ProcessBuilder("java", clazz,"argument");

Gaurav Sharma
  • 745
  • 7
  • 23