通过Runtime.getRuntime().exec() 调用系统应用时,应该异步打印verbose信息

如果你这样打, 可能会导致执行阻塞

	private static void printInput(Process p) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
		String line;
		while ((line = in.readLine()) != null) {
			System.out.println(line);
		}
		in.close();
	}

所以,你要用非阻塞的方式来打。CXF源代码里有个不错的例子:

        if (p.getErrorStream() != null) {
            StreamPrinter errorStreamPrinter = new StreamPrinter(p.getErrorStream(), "", System.out);
            errorStreamPrinter.start();  //StreamPrinter是一个线程
        }

        if (p.getInputStream() != null) {
            StreamPrinter infoStreamPrinter = new StreamPrinter(p.getInputStream(), "[INFO]", System.out);
            infoStreamPrinter.start();  //StreamPrinter是一个线程
        }

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.