BufferedReader#readLine``null到达流的末尾时将返回。
因为您基本上是在忽略此退出指示器并无限循环地循环,所以您得到的只是
null。
可能的原因是因为该进程已向错误流输出了一些错误信息,而您没有阅读这些信息。
您应该尝试使用
ProcessBuilder代替,这使您可以将错误流重定向到输入流中。
try { String[] command = {"java.exe", "mytest.DDD"}; ProcessBuilder pb = new ProcessBuilder(command); // Use this if the place you are running from (start context) // is not the same location as the top level of your classes //pb.directory(new File("pathtoyourclasses")); pb.redirectErrorStream(true); Process exec = pb.start(); BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream())); String text = null; while ((text = br.readLine()) != null) { System.out.println(text); }} catch (IOException exp) { exp.printStackTrace();}
ps-如果
java.exe您的路径有效,则可以使用,否则,您将需要提供可执行文件的完整路径,就像您在示例中已经完成的一样;)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)