使用runtime.getruntime.exec从Java调用python脚本 [英] Calling python script from Java using runtime.getruntime.exec
问题描述
我有一个java web开发项目,想要调用python脚本在后台运行,然后继续使用java。
I have a java web development project, and want to call a python script to run in the background and then carry on with the java.
String command = "cmd.exe /c cd "C:\\path\\to\\py\\" && python script.py";
Process p = Runtime.getRuntime().exec(command);
当我调用它时似乎没有发生任何事情,但我需要在脚本访问文件时首先更改目录在其目录中。
Nothing seems to happen when i call this, but i need to change directory first as the script accesses files in its directory.
感谢您的帮助
编辑:
正确的答案是添加开始,这是我编辑的代码
Correct answer was adding start, this is my edited code
String command = "cmd.exe /c cd "C:\\path\\to\\py\\" && start python script.py";
Process p = Runtime.getRuntime().exec(command);
推荐答案
您是否配置了环境以支持可执行python脚本?
如果没有,你应该这样调用:
Did you configure your environment to support "executable" python scripts?
If not, you should call it like this:
String command = "cmd.exe /c start python path\to\script\script.py";
Process p = Runtime.getRuntime().exec(command);
start
命令运行相应的可执行文件(在这种情况下,python解释器),提供其参数(在本例中为脚本本身)。
The start
command runs the appropriate executable (in this case python interpreter), with its supplied arguments (in this case the script itself).
这篇关于使用runtime.getruntime.exec从Java调用python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!