从另一个java程序运行java程序

Ans*_*imu 13 java runtime runtime.exec

我正在研究一个简单的java程序.它只是编译并执行另一个java程序.我正在使用Runtime.exec()函数进行编译和运行.编译没有问题.但是当它运行时,如果第二个程序需要输入来从键盘读取,我不能从主进程中提供它.我使用了getOutputStream()函数.但它无能为力.我会提供我的代码.

public class sam {  
    public static void main(String[] args) throws Exception {  
        try { 
             Process p = Runtime.getRuntime().exec("javac sam2.java");
             Process p2 = Runtime.getRuntime().exec("java sam2");
             BufferedReader in = new BufferedReader(  
                                new InputStreamReader(p2.getInputStream()));

             OutputStream out = p.getOutputStream();
             String line = null; 
             line = in.readLine();
             System.out.println(line);
             input=input+"\n";
             out.write(input.getBytes());
             p.wait(10000);
             out.flush();
        }catch (IOException e) {  
             e.printStackTrace();  
        }  
    }  
}  
Run Code Online (Sandbox Code Playgroud)

这是我的主程序(sam.java).

以下是sam2.java的代码

public class sam2 {  
public static void main(String[] args) throws Exception {  

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str; 
    System.out.println("Enter the number..\n");
    str = br.readLine(); 
    System.out.println(Integer.parseInt(str));

    }  
}  
Run Code Online (Sandbox Code Playgroud)

如果我的第二个程序只有打印语句,那就没问题了.但是当我必须从另一个人那里读到一些东西时,问题就出现了.

小智 21

这有点奇怪,但你可以运行第二个程序而不需要它.只需调用其中的main方法即可.所以忘记运行时部分并执行以下操作:

sam2.main(new String[0]);
Run Code Online (Sandbox Code Playgroud)

当然,这种方式必须在编译时编译sam2


Mad*_*mer 12

需要允许每个进程运行和完成.你可以用它Process#waitFor来达到这个目的.同样,您需要同时使用进程的任何输出. waitFor将阻塞所以你需要使用a Thread来读取输入(如果需要,将输出写入进程)

根据java/class文件的位置,您可能还需要指定一个起始文件夹,可以从该文件夹开始执行该过程.

大多数这使用起来更容易 ProcessBuilder

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

显然,你应该检查进程的返回结果,并且可以产生一个更好的与进程交互的机制,但这是基本的想法......