详解java调用python的几种用法(看这篇就够了)
作者:不懒人 时间:2023-04-10 22:15:57
java调用python的几种用法如下:
在java类中直接执行python语句
在java类中直接调用本地python脚本
使用Runtime.getRuntime()执行python脚本文件(推荐)
调用python脚本中的函数
准备工作:
创建maven工程,结构如下:
到官网https://www.jython.org/download.html下载Jython的jar包或者在maven的pom.xml文件中加入如下代码:
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.0</version>
</dependency>
1.在java类中直接执行python语句
创建JavaRunPython.java类:
package com.test;
import org.python.util.PythonInterpreter;
public class JavaRunPython {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("a='hello world'; ");
interpreter.exec("print a;");
}
}
输出结果如下:
出现的console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.并不是错误,而是兼容所导致,解决方法如下:
2.在java中直接调用python脚本
在本地的D盘创建一个python脚本,文件名字为javaPythonFile.py,文件内容如下:
a = 1
b = 2
print (a + b)
创建JavaPythonFile.java类,内容如下:
package com.test;
import org.python.util.PythonInterpreter;
public class JavaPythonFile {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("D:\\javaPythonFile.py");
}
}
输出结果如下:
3.使用Runtime.getRuntime()执行python脚本文件,推荐使用
在本地的D盘创建一个python脚本,文件名字为Runtime.py,文件内容如下:
print('RuntimeDemo')
创建RuntimeFunction.java类,内容如下:
package com.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RuntimeFunction {
public static void main(String[] args) {
Process proc;
try {
proc = Runtime.getRuntime().exec("python D:\\Runtime.py");
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();
}
}
}
运行结果如下:
4.调用python脚本中的函数
在本地的D盘创建一个python脚本,文件名字为add.py,文件内容如下:
def add(a,b):
return a + b
创建Function.java类,内容如下:
package com.test;
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class Function {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("D:\\add.py");
// 第一个参数为期望获得的函数(变量)的名字,第二个参数为期望返回的对象类型
PyFunction pyFunction = interpreter.get("add", PyFunction.class);
int a = 5, b = 10;
//调用函数,如果函数需要参数,在Java中必须先将参数转化为对应的“Python类型”
PyObject pyobj = pyFunction.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("the anwser is: " + pyobj);
}
}
运行结果如下:
来源:https://www.cnblogs.com/wuwuyong/p/10600749.html
标签:java,调用,python
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java数据结构之二叉排序树的实现
2023-07-05 02:27:25
![](https://img.aspxhome.com/file/2023/0/61600_0s.png)
Android 监听手机GPS打开状态实现代码
2022-09-28 08:56:38
C#中Clone一个对象的值到另一个对象案例
2022-10-11 21:41:14
SpringBoot中异常处理实战记录
2022-07-21 20:39:01
![](https://img.aspxhome.com/file/2023/2/121932_0s.png)
java中treemap和treeset实现红黑树
2021-12-05 07:22:29
![](https://img.aspxhome.com/file/2023/3/73043_0s.png)
JAVA WSIMPORT生成WEBSERVICE客户端401认证过程图解
2023-11-14 00:27:55
![](https://img.aspxhome.com/file/2023/5/58985_0s.png)
Java监听器的作用及用法代码示例
2023-06-24 06:59:07
![](https://img.aspxhome.com/file/2023/1/68741_0s.png)
SpringBoot2整合Redis实现读写操作
2023-08-03 08:19:24
![](https://img.aspxhome.com/file/2023/5/105925_0s.png)
C# WinForm打开PDF文件并在窗体中显示
2023-11-14 10:47:49
Android实现按钮点击效果
2021-06-10 10:05:54
误将.idea文件提交至git后删除的操作方法
2022-09-03 09:32:57
C#将Word转换成PDF方法汇总(基于Office和WPS)
2021-09-12 22:52:58
java注解的类型知识点总结
2022-11-04 00:26:02
SpringBoot @Validated注解实现参数分组校验的方法实例
2023-01-02 11:18:50
![](https://img.aspxhome.com/file/2023/3/63633_0s.png)
Go Java算法之从英文中重建数字示例详解
2023-10-25 02:37:26
android如何获取联系人所有信息
2021-10-24 13:24:41
Java动态获取实现某个接口下所有的实现类对象集合
2023-04-01 14:43:20
递归法求最大公约数和最小公倍数的实现代码
2023-10-07 02:11:34
解读@RabbitListener起作用的原理
2021-08-04 12:58:59
Java线程间的通信方式详解
2022-05-20 10:33:47