执行命令到进程

时间:2014-01-24 03:40:41

标签: java process runtime

所以我有这段代码:

String[] command = new String[] { "java", "-jar", file.getName() };
try {
    System.out.println("Loading input/output");
    final Process process = Runtime.getRuntime().exec(command);
    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
    final OutputStreamWriter out = new OutputStreamWriter(process.getOutputStream());
    final BufferedReader consolein = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Loaded input/output");
    new Thread() {
        @Override
        public void run() {
            while (true) {
                System.out.println("hi");
                try {
                    String temp = consolein.readLine();
                    out.write(temp);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();
    while (true) {
        System.out.println(in.readLine());
    }
} catch (IOException e) {
    e.printStackTrace();
}

我正在尝试对正在运行的jar执行命令。输入很好,我在控制台中看到所有消息。我添加了“hi”消息来测试它是否正常工作。每当我输入内容时它就会出现。但是,无论我输入什么,都不会被发送到正在运行的程序。我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

根据您所使用的流程预期,您可能不需要新行;您可能只需要拨打flush()上的OutputStreamWriter

String temp = consolein.readLine();
out.write(temp);    
out.flush();

请参阅http://docs.oracle.com/javase/7/docs/api/java/io/OutputStreamWriter.html

答案 1 :(得分:0)

我做了一个快速测试。我写了这个小程序,由我的其他班级运行......

import java.util.Scanner;

public class RunMe {

    public static void main(String[] args) {
        System.out.println("Hello world");
        Scanner scanner = new Scanner(System.in);
        String text = scanner.nextLine();
        System.out.println("You said: " + text);
        System.out.println("Bye!");
    }

}

我写这篇文章来运行它......

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Runner {

    private static BufferedReader br;
    private static BufferedWriter bw;

    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder("java", "-jar", "RunMe.jar");
        pb.redirectError();

        Scanner scanner = new Scanner(System.in);
        try {
            Process p = pb.start();
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            bw = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        String text = null;
                        while ((text = br.readLine()) != null) {
                            System.out.println(text);
                        }
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }).start();

            String text = scanner.nextLine();
            bw.write(text);
            bw.newLine();
            bw.flush();

            p.waitFor();

        } catch (IOException | InterruptedException ex) {
            ex.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (Exception e) {
            }
            try {
                bw.close();
            } catch (Exception e) {
            }
        }
    }

}

它输出......

Hello world
This is a test
You said: This is a test
Bye!

(第二行是我输入的内容,第三行是从RunMe类输出的。)

所以我建议你需要发一条新线。您可以尝试使用\n,但最好使用line.separator属性