[英]Java start python process
I'm trying to start a python script via java and send the data via socket back to the java program. 我正在尝试通过java启动python脚本,并通过套接字将数据发送回java程序。 My problem is that when I start the python script after the SocketServer is created with
Runtime.getRuntime().exec("python3 ~/Documents/sensor/sensorADC.py");
我的问题是,在使用
Runtime.getRuntime().exec("python3 ~/Documents/sensor/sensorADC.py");
创建SocketServer之后启动python脚本时Runtime.getRuntime().exec("python3 ~/Documents/sensor/sensorADC.py");
no connection is established and no data is transmitted. 没有建立连接,也没有数据传输。
But when I start the script manually via CLI, everything works fine. 但是当我通过CLI手动启动脚本时,一切正常。
The timing of the program start shouldn't be the problem, because I already tried different ways and orders. 程序启动的时间不应该是问题,因为我已经尝试了不同的方法和命令。
The java program is a javaFx application. java程序是一个javaFx应用程序。 EDIT: tested in Linux
编辑:在Linux中测试
2nd Edit: private void startPythonScript () { try { measureProcess = Runtime.getRuntime().exec("python3 ~/Documents/sensor/sensorADC.py")); } catch (IOException e) { e.printStackTrace(); } }
2nd Edit:
private void startPythonScript () { try { measureProcess = Runtime.getRuntime().exec("python3 ~/Documents/sensor/sensorADC.py")); } catch (IOException e) { e.printStackTrace(); } }
private void startPythonScript () { try { measureProcess = Runtime.getRuntime().exec("python3 ~/Documents/sensor/sensorADC.py")); } catch (IOException e) { e.printStackTrace(); } }
What am I missing? 我错过了什么?
Thank you in advance 先感谢您
You're close, you can pass an array to the exec method, like this: 你很接近,你可以将一个数组传递给exec方法,如下所示:
String[] cmd = { "python3", "sensorADC.py", "~/Documents/sensor/"};
Runtime.getRuntime().exec(cmd);
and it should work. 它应该工作。
I "solved" it. 我“解决了”它。 I compiled it with pyinstaller and now it works.
我用pyinstaller编译它现在它的工作原理。 Python seems to be the problem here.
Python似乎是这里的问题。 But I don't know why....
但我不知道为什么....
For others I compiled it to an executable with pyinstaller --onefile sensor.py
对于其他人,我使用
pyinstaller --onefile sensor.py
将其编译为可执行文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.