如何使用运行时在java中调用python程序
我想用java中的参数调用python程序。但我的输出是空白的。代码在这里 Python代码如下:如何使用运行时在java中调用python程序,java,python,runtime,Java,Python,Runtime,我想用java中的参数调用python程序。但我的输出是空白的。代码在这里 Python代码如下: import sys print(sys.argv[1]) public class PrintNumber{ public static void main(String[] args){ Process proc; try { proc = Runtime.getRuntime().exec("python ../python
import sys
print(sys.argv[1])
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();
}
}
}
java代码如下:
import sys
print(sys.argv[1])
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);
参考:的副本?可能找不到Python脚本。为了进行调查,您可能会阅读
proc.getErrorStream
以查看任何错误。@次优是的,您完全正确,谢谢。我解决了这个问题,我的问题是我无法运行导入第三方库的python代码。所以我需要找到python的全名,这意味着我应该首先在终端中编写“whichpython”,然后写下完整的路径。在本例中,我应该写下“/Users/archer/anaconda3/bin/python../pythonProgram/pythonProgram/PrintN.py 30”,而不是“pythonProgram/pythonProgram/PrintN.py 30”,第一个参数是“which python”之后的结果