(转)java调用python脚本
这篇博客旨在吐血分享今天遇到的java调用python脚本遇到的坑,折腾了3个多小时终于可以跑通了,代码超级短,但网上的好多资料都是抄来抄去的,很少有能够直接跑通的,尤其是针对你的python文件中用到第三方类库的情况。
先来说说我为什么要用java调用python代码,原因就在于python在爬虫方面提供了丰富的类库,但我本来已经有一套java操作数据库以及消息中间件的代码,这些代码用python实现的话可能需要至少一周时间,为了减少时间成本,因此我决定用java调用python脚本的方式使用requests等等库,下面把我解决的过程分享出来,希望能给遇到类似问题的朋友一点帮助。
常见的java调用python脚本方式
- 通过jython.jar提供的类库实现
- 通过Runtime.getRuntime()开启进程来执行脚本文件
通过jython.jar提供的类库实现
通过jython.jar实现的话,我们需要引入jar包,具体我写了一个demo,假设你的python代码为test.py:
1 2 3 4 | def my_test(name, age): print ( "name: " + name) print ( "age: " + age) return "success" |
java调用test.py代码:
1 2 3 4 5 6 7 | public static void main(String[] args) { PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile( "E:\\workspace\\pycharm_workspace\\weixincrawer\\test.py" ); PyFunction function = (PyFunction)interpreter.get( "my_test" ,PyFunction. class ); PyObject pyobject = function.__call__( new PyString( "huzhiwei" ), new PyString( "25" )); System.out.println( "anwser = " + pyobject.toString()); } |
输出结果:
name: huzhiwei
age: 25
anwser = success
到此是没有什么问题的,我们使用function.call方法传入参数调用python函数,使用pyobject.toString()方法拿到python中my_test函数的返回值,但是如果你把test.py稍微做下修改如下:
1 2 3 4 5 6 7 | import requests def my_test(name, age): response = requests.get( "http://www.baidu.com" ) print ( "name: " + name) print ( "age: " + age) return "success" |
不修改java调用代码的情况下,你会得到下面异常信息:
ImportError: No module named requests
没错,这就是我要讨论的问题,因为jython不可能涵盖所有python第三方类库的东西,所以在我们得python文件中用到requests类库的时候,很显然会报找不到模块的错误,这个时候我们是可以通过Runtime.getRuntime()开启进程来执行python脚本文件的。
通过Runtime.getRuntime()开启进程来执行脚本文件
使用这种方式需要同时修改python文件以及java调用代码,在此我同样在上面test.py的基础上进行修改:
1 2 3 4 5 6 7 8 9 10 11 | import requests import sys def my_test(name, age): response = requests.get( "http://www.baidu.com" ) print ( "url:" + response.url) print ( "name: " + name) print ( "age: " + age) return "success" my_test(sys.argv[ 1 ], sys.argv[ 2 ]) |
和上面test.py代码最大的区别在于,我们此处开启进程的方式实际上是在隐形的调用dos界面进行操作,因此在python代码中我们需要通过sys.argv的方式来拿到java代码中传递过来的参数。
java调用代码部分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public static void main(String[] args) { String[] arguments = new String[] { "python" , "E:\\workspace\\pycharm_workspace\\weixincrawer\\test.py" , "huzhiwei" , "25" }; try { Process process = Runtime.getRuntime().exec(arguments); BufferedReader in = new BufferedReader( new InputStreamReader(process.getInputStream())); String line = null ; while ((line = in.readLine()) != null ) { System.out.println(line); } in.close(); int re = process.waitFor(); System.out.println(re); } catch (Exception e) { e.printStackTrace(); } } |
结果输出:
url:http://www.baidu.com/
name: huzhiwei
age: 25
0
在此需要注意的一点,java代码中的process.waitFor()返回值为0表示我们调用python脚本成功,返回值为1表示调用python脚本失败,这和我们通常意义上见到的0与1定义正好相反。
我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2018-02-27 # @Author : zhangmingcheng from PIL import Image import imagehash import sys def get_hash(path): image = Image. open (path) h = str (imagehash.dhash(image)) print h return h get_hash(sys.argv[ 1 ]) |
java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /** * 获取影像dhash,获取完后将影像删除 */ @Override public String getDhash(String absolutePathName) { String dhash = "" ; String[] arguments = new String[] { "python" , "/root/imagedhash/rest.py" , absolutePathName }; try { Process process = Runtime.getRuntime().exec(arguments); BufferedReader in = new BufferedReader( new InputStreamReader(process.getInputStream())); String line = null ; while ((line = in.readLine()) != null ) { dhash = line; } in.close(); process.waitFor(); } catch (Exception e) { e.printStackTrace(); } File file = new File(absolutePathName); if (file.exists() && file.isFile()) { file.delete(); System.out.println(absolutePathName + "影像的dhash值=" + dhash + ",删除影像成功" ); } return dhash; } |
转自:http://blog.csdn.net/hzw19920329/article/details/77509497
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构