如何在Java中运行python

如何在Java中运行python

本文介绍了如何在Java中运行python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用Jython的情况下在Java程序中运行文件* .py *?例如:我有JButton,当我单击JButton时,我想运行python脚本.在不使用Jython的情况下,我应该在动作执行"按钮中添加什么以运行python脚本?

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?

推荐答案

您需要在要执行此操作的计算机中安装python解释器,然后将该解释器作为Java的外部命令来调用.

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.

看看这个问题,以了解有关如何执行该调用的更多信息:在Java中执行外部程序

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

从那里:

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

此外,您可以使用返回的 Process exec中的对象与您的脚本输入/输出进行交互.

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

这篇关于如何在Java中运行python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:07