我想使用来自 java 的参数调用 python 程序。但我的输出是空白的。代码在这里。
Python 代码在这里:
import sys
print(sys.argv[1])
java代码在这里:
public class PrintNumber{
public static void main(String[] args){
Process proc;
try {
proc = Runtime.getRuntime().exec("python ../pythonProgram/pythonProgram/PrintN.py 30");
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
proc.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
我想输出30,有人能告诉我错在哪里吗?
最佳答案
你可以尝试一下:
String command = "python /c start python ../pythonProgram/pythonProgram/PrintN.py";
int param = 30;
proc = Runtime.getRuntime().exec(command + param);
关于java - 如何使用Runtime在java中调用python程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58909192/