我想用run.exec在java代码中执行python,并期望用户在python中输入。当我在intelliJ中运行java时,它无法获取输入并且什么都不打印。作为代码:
input.py:
def input():
print "hello"
w=int(raw_input('input weight:'))
h=int(raw_input('input height:'))
s=w*h
print s
if __name__=='__main__':
print 'main'
input()
java代码:
Process pr = Runtime.getRuntime().exec("python input.py");
InputStream is = pr.getInputStream();
BufferedReader stdError = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
String line;
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
System.out.println(line);
}
while ((line=stdError.readLine())!=null){
System.out.println(line);
}
pr.waitFor();