Python 官方文档:入门教程 => 点击学习
在java中调用python脚本 推荐使用第三种方法,因为只有第三种方法使用Runtime.getRuntime()才能执行含有第三方库(numpy,matlab,pandas等库)的Python脚本
推荐使用第三种方法,因为只有第三种方法使用Runtime.getRuntime()
才能执行含有第三方库(numpy,matlab,pandas等库)的Python脚本。
1.首先在maven中添加依赖
<dependency> <groupId>org.python</groupId> <artifactId>jython-standalone</artifactId> <version>2.7.0</version></dependency>
2.使用Jpython中的PythonInterpreter执行Python语句
public class Tool{ public static void main(String [] args){ PythonInterpreter interpreter = new PythonInterpreter();interpreter.exec("print("This is a JPython text")"); interpreter.exec("print(2+3)"); }}
1.首先在maven中添加依赖(也是依赖Jpython包)
<dependency> <groupId>org.python</groupId> <artifactId>jython-standalone</artifactId> <version>2.7.0</version></dependency>
2.创建一个Python脚本
#当我们的Python脚本中有汉字的时候,需要在脚本的第一行写coding = utf-8 来告诉编译器编码方式是什么# -*- coding: UTF-8 -*-a = 'This is a test'print(a)
3.在java中执行python脚本
public class Tool{ public static void main(String [] args){ PythonInterpreter interpreter = new PythonInterpreter();interpreter.execfile("./text.py");//放脚本的位置interpreter.cleanup();interpreter.close(); }}
先创建一个简单的调用第三方库的Python脚本
import numpy as npa = np.arange(10)print(a)
然后使用 Runtime.getRuntime() 方法执行python脚本
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader; public class Tool { public static void main(String[] args) { try { Process proc = Runtime.getRuntime().exec("test.py");//执行脚本 //用输入输出流来截取结果 BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream())); String line = null; while((line = reader.readLine()) != null){ System.out.println(line); } reader.close(); proc.waitFor(); } catch (IOException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } }}
import sysdef sum(a, b, c):return a+b+cif __name__ == "__main__":a=(int(sys.argv[1]))b=(int(sys.argv[2]))c=(int(sys.argv[3]))s=sum(a,b,c)print("finish!!!")print(s)
sys.argv用于获取参数url1,url2,乃至更多,sys.argv[0]代表python程序名
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Scanner;public class Tool { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); int a = scanner.nextInt();int b = scanner.nextInt();int c = scanner.nextInt(); try { String[] my_args =new String[] {"python","test.py",String.valueOf(a),String.valueOf(b),String.valueOf(c)}; Process proc = Runtime.getRuntime().exec(my_args);//执行脚本 BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream())); String line = null; while((line = reader.readLine()) != null){ System.out.println(line); } reader.close(); proc.waitFor(); } catch (IOException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } }}
my_args数组里面存放的参数{“python”,“test.py”,String.valueOf(a),String.valueOf(b),String.valueOf©},第一个是固定的就写’python‘,第二个是我们要执行的python脚本的位置(注意路径),后面的是我们要传递的参数也就是url1,url2等等(和Python脚本所接收的内容互相对应)
这种方式我们需要使用输入流输出流BufferedReader来截取结果
来源地址:https://blog.csdn.net/weixin_48958956/article/details/128333340
--结束END--
本文标题: 在java中调用python脚本
本文链接: https://www.lsjlt.com/news/398830.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
一口价域名售卖能注册吗?域名是网站的标识,简短且易于记忆,为在线用户提供了访问我们网站的简单路径。一口价是在域名交易中一种常见的模式,而这种通常是针对已经被注册的域名转售给其他人的一种方式。
一口价域名买卖的过程通常包括以下几个步骤:
1.寻找:买家需要在域名售卖平台上找到心仪的一口价域名。平台通常会为每个可售的域名提供详细的描述,包括价格、年龄、流
443px" 443px) https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294.jpg https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294-768x413.jpg 域名售卖 域名一口价售卖 游戏音频 赋值/切片 框架优势 评估指南 项目规模
0