我有以下短Python程序“test.py”的Runtime.exec()在Java挂起因为它是从System.in
n = int(raw_input())
print n
我是从下面的Java执行上述程序节目 “ProcessRunner.java”
import java.util.*;
import java.io.*;
public class ProcessRunner {
public static void main(String[] args) {
try {
Scanner s = new Scanner(Runtime.getRuntime().exec("python test.py").getInputStream()).useDelimiter("\\A");
System.out.println(s.next());
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
在运行命令,
java ProcessRunner
我不能够通过在亲值 'N'每种格式的Python程序和Java运行挂起。什么是正确的方式来处理这种情况,并从java程序中动态地将值'n'传递给python程序?
看看这里http://stackoverflow.com/questions/18903549/writing-to-inputstream-of-a-java-process关于如何与Python进程进行通信的信息。 –
另请参阅[当Runtime.exec()不会](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html)很多很好的有关正确创建和处理过程的提示。然后忽略它是指'exec'并使用'ProcessBuilder'来创建进程。还要将'String arg'分解为'String [] args'来解释包含空格字符的路径。 –