How to run python in java

一世执手 提交于 2020-01-06 04:17:25

问题


How to run file*.py* in your Java program without using Jython? For example: I have JButton and I want to run python script when I click on the JButton. What should I put in Action Preformed for the button to run python script without using Jython?


回答1:


You need to have the python interpreter installed in the machine where you want to do that and call this interpreter as an external command from Java.

Look at this question to know more about how perform that call: Execute external program in java

From there:

String[] params = new String [2];
params[0] = PATH2_YOUR_PYTHON_SETUP + "python.exe";
params[1] = PATH2_YOUR_PYTHON_SCRIPT;
Runtime.getRuntime().exec(params);

Additionally, you can use the returned Process object from exec to interact with your script input/output.



来源:https://stackoverflow.com/questions/22686339/how-to-run-python-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!