Compile and Run Java Program from another Java Program [closed]

无人久伴 提交于 2019-12-02 22:34:03

问题


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

回答1:


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




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/17492030/compile-and-run-java-program-from-another-java-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!