Java调用Python程序方法总结(最全最详细)
原文:https://blog.csdn.net/IT_xiao_bai/article/details/79074988
如何使用Java调用Python程序
本文为大家介绍如何java调用python方法,供大家参考。
实际工程项目中可能会用到Java和python两种语言结合进行,这样就会涉及到一个问题,就是怎么用Java程序来调用已经写好的python脚本呢,一共有三种方法可以实现,具体方法分别为大家介绍:
1. 在java类中直接执行python语句
此方法需要引用org.python包,需要下载Jpython。在这里先介绍一下Jpython。下面引入百科的解释:
- Jython是一种完整的语言,而不是一个Java翻译器或仅仅是一个Python编译器,它是一个Python语言在Java中的完全实现。Jython也有很多从CPython中继承的模块库。最有趣的事情是Jython不像CPython或其他任何高级语言,它提供了对其实现语言的一切存取。所以Jython不仅给你提供了Python的库,同时也提供了所有的Java类。这使其有一个巨大的资源库。
这里我建议下载最新版本的Jpython,因为可以使用的python函数库会比老版本的多些,目前最新版本为2.7。
下载jar包请点击Download Jython 2.7.0 - Standalone Jar
下载安装程序请点击Download Jython 2.7.0 - Installer
如果使用maven依赖添加的话,使用下面的语句
<span style="color:#000000"><code class="language-maven"><dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.0</version>
</dependency>
</code></span>
以上准备好了,就可以直接在java类中写python语句了,具体代码如下:
<span style="color:#000000"><code class="language-java">PythonInterpreter interpreter <span style="color:#df5000">=</span> <span style="color:#a71d5d">new</span> PythonInterpreter<span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
interpreter<span style="color:#969896">.</span><span style="color:#795da3">exec</span><span style="color:#969896">(</span><span style="color:#df5000">"a=[5,2,3,9,4,0]; "</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
interpreter<span style="color:#969896">.</span><span style="color:#795da3">exec</span><span style="color:#969896">(</span><span style="color:#df5000">"print(sorted(a));"</span><span style="color:#969896">)</span><span style="color:#969896">;</span> <span style="color:#969896">//此处python语句是3.x版本的语法</span>
interpreter<span style="color:#969896">.</span><span style="color:#795da3">exec</span><span style="color:#969896">(</span><span style="color:#df5000">"print sorted(a);"</span><span style="color:#969896">)</span><span style="color:#969896">;</span> <span style="color:#969896">//此处是python语句是2.x版本的语法</span>
</code></span>
输出结果如下:
这里会看到输出的结果都是一样的,也就是说Jpython兼容python2.x和3.x版本的语句,运行速度会比直接运行python程序稍慢一点。
但是每次运行结果都会提示console: Failed to install ‘’: java.nio.charset.UnsupportedCharsetException: cp0. 这样看起来很烦,因为每次运行结果都会出现红色的提示语句,以为是错误,程序员应该都不愿意看到这一幕,得想个办法解决。
解决方法如下:
在要执行的代码上右键, Run As>Run Configurations,选择第二个页签Arguments,在VM arguments中添加以下语句
-Dpython.console.encoding=UTF-8
然后Apply->Run就可以了。
如下图所示:
2. 在java中调用本地python脚本
首先在本地建立一个python脚本,命名为add.py,写了一个简单的两个数做加法的函数,代码如下:
<span style="color:#000000"><code class="language-python"><span style="color:#a71d5d">def</span> <span style="color:#795da3">add</span><span style="color:#969896">(</span>a<span style="color:#969896">,</span>b<span style="color:#969896">)</span><span style="color:#969896">:</span>
<span style="color:#a71d5d">return</span> a <span style="color:#df5000">+</span> b
</code></span>
python的功能函数已经写好,接下来我们写一个java的测试类(同样需要用到Jpython包),来测试一下是否可以运行成功。代码如下:
<span style="color:#000000"><code class="language-java"><span style="color:#a71d5d">import</span> org<span style="color:#969896">.</span>python<span style="color:#969896">.</span>core<span style="color:#969896">.</span>PyFunction<span style="color:#969896">;</span>
<span style="color:#a71d5d">import</span> org<span style="color:#969896">.</span>python<span style="color:#969896">.</span>core<span style="color:#969896">.</span>PyInteger<span style="color:#969896">;</span>
<span style="color:#a71d5d">import</span> org<span style="color:#969896">.</span>python<span style="color:#969896">.</span>core<span style="color:#969896">.</span>PyObject<span style="color:#969896">;</span>
<span style="color:#a71d5d">import</span> org<span style="color:#969896">.</span>python<span style="color:#969896">.</span>util<span style="color:#969896">.</span>PythonInterpreter<span style="color:#969896">;</span>
<span style="color:#a71d5d">public</span> <span style="color:#a71d5d">class</span> Java_Python_test <span style="color:#969896">{</span>
<span style="color:#a71d5d">public</span> <span style="color:#a71d5d">static</span> <span style="color:#a71d5d">void</span> <span style="color:#795da3">main</span><span style="color:#969896">(</span>String<span style="color:#969896">[</span><span style="color:#969896">]</span> args<span style="color:#969896">)</span> <span style="color:#969896">{</span>
<span style="color:#969896">// TODO Auto-generated method stub</span>
PythonInterpreter interpreter <span style="color:#df5000">=</span> <span style="color:#a71d5d">new</span> PythonInterpreter<span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
interpreter<span style="color:#969896">.</span><span style="color:#795da3">execfile</span><span style="color:#969896">(</span><span style="color:#df5000">"D:\\add.py"</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
<span style="color:#969896">// 第一个参数为期望获得的函数(变量)的名字,第二个参数为期望返回的对象类型</span>
PyFunction pyFunction <span style="color:#df5000">=</span> interpreter<span style="color:#969896">.</span><span style="color:#795da3">get</span><span style="color:#969896">(</span><span style="color:#df5000">"add"</span><span style="color:#969896">,</span> PyFunction<span style="color:#969896">.</span><span style="color:#a71d5d">class</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
<span style="color:#a71d5d">int</span> a <span style="color:#df5000">=</span> <span style="color:#c76b29">5</span><span style="color:#969896">,</span> b <span style="color:#df5000">=</span> <span style="color:#c76b29">10</span><span style="color:#969896">;</span>
<span style="color:#969896">//调用函数,如果函数需要参数,在Java中必须先将参数转化为对应的“Python类型”</span>
PyObject pyobj <span style="color:#df5000">=</span> pyFunction<span style="color:#969896">.</span><span style="color:#795da3">__call__</span><span style="color:#969896">(</span><span style="color:#a71d5d">new</span> PyInteger<span style="color:#969896">(</span>a<span style="color:#969896">)</span><span style="color:#969896">,</span> <span style="color:#a71d5d">new</span> PyInteger<span style="color:#969896">(</span>b<span style="color:#969896">)</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
System<span style="color:#969896">.</span>out<span style="color:#969896">.</span><span style="color:#795da3">println</span><span style="color:#969896">(</span><span style="color:#df5000">"the anwser is: "</span> <span style="color:#df5000">+</span> pyobj<span style="color:#969896">)</span><span style="color:#969896">;</span>
<span style="color:#969896">}</span>
<span style="color:#969896">}</span>
</code></span>
运行结果如图所示:
OK,测试成功了~
关于Jpython更多详细的信息可以参考官方的相关文档,官网地址点这里。
注意:以上两个方法虽然都可以调用python程序,但是使用Jpython调用的python库不是很多,如果你用以上两个方法调用,而python的程序中使用到第三方库,这时就会报错java ImportError: No module named xxx。遇到这种情况推荐使用下面的方法,即可解决该问题。
3. 使用Runtime.getRuntime()执行脚本文件(推荐)
为了验证该方法可以运行含有python第三方库的程序,我们先写一个简单的python脚本,代码如下:
<span style="color:#000000"><code class="language-python"><span style="color:#a71d5d">import</span> numpy <span style="color:#a71d5d">as</span> np
a <span style="color:#df5000">=</span> np<span style="color:#969896">.</span>arange<span style="color:#969896">(</span><span style="color:#c76b29">12</span><span style="color:#969896">)</span><span style="color:#969896">.</span>reshape<span style="color:#969896">(</span><span style="color:#c76b29">3</span><span style="color:#969896">,</span><span style="color:#c76b29">4</span><span style="color:#969896">)</span>
<span style="color:#a71d5d">print</span><span style="color:#969896">(</span>a<span style="color:#969896">)</span>
</code></span>
可以看到程序中用到了numpy第三方库,并初始化了一个3×4的一个矩阵。
下面来看看怎么用Runtime.getRuntime()方法来调用python程序并输出该结果,java代码如下:
<span style="color:#000000"><code class="language-java"><span style="color:#a71d5d">import</span> java<span style="color:#969896">.</span>io<span style="color:#969896">.</span>BufferedReader<span style="color:#969896">;</span>
<span style="color:#a71d5d">import</span> java<span style="color:#969896">.</span>io<span style="color:#969896">.</span>IOException<span style="color:#969896">;</span>
<span style="color:#a71d5d">import</span> java<span style="color:#969896">.</span>io<span style="color:#969896">.</span>InputStreamReader<span style="color:#969896">;</span>
<span style="color:#a71d5d">public</span> <span style="color:#a71d5d">class</span> Demo1 <span style="color:#969896">{</span>
<span style="color:#a71d5d">public</span> <span style="color:#a71d5d">static</span> <span style="color:#a71d5d">void</span> <span style="color:#795da3">main</span><span style="color:#969896">(</span>String<span style="color:#969896">[</span><span style="color:#969896">]</span> args<span style="color:#969896">)</span> <span style="color:#969896">{</span>
<span style="color:#969896">// TODO Auto-generated method stub</span>
Process proc<span style="color:#969896">;</span>
<span style="color:#a71d5d">try</span> <span style="color:#969896">{</span>
proc <span style="color:#df5000">=</span> Runtime<span style="color:#969896">.</span><span style="color:#795da3">getRuntime</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">.</span><span style="color:#795da3">exec</span><span style="color:#969896">(</span><span style="color:#df5000">"python D:\\demo1.py"</span><span style="color:#969896">)</span><span style="color:#969896">;</span><span style="color:#969896">// 执行py文件</span>
<span style="color:#969896">//用输入输出流来截取结果</span>
BufferedReader in <span style="color:#df5000">=</span> <span style="color:#a71d5d">new</span> BufferedReader<span style="color:#969896">(</span><span style="color:#a71d5d">new</span> InputStreamReader<span style="color:#969896">(</span>proc<span style="color:#969896">.</span><span style="color:#795da3">getInputStream</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">)</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
String line <span style="color:#df5000">=</span> null<span style="color:#969896">;</span>
<span style="color:#a71d5d">while</span> <span style="color:#969896">(</span><span style="color:#969896">(</span>line <span style="color:#df5000">=</span> in<span style="color:#969896">.</span><span style="color:#795da3">readLine</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">)</span> <span style="color:#df5000">!=</span> null<span style="color:#969896">)</span> <span style="color:#969896">{</span>
System<span style="color:#969896">.</span>out<span style="color:#969896">.</span><span style="color:#795da3">println</span><span style="color:#969896">(</span>line<span style="color:#969896">)</span><span style="color:#969896">;</span>
<span style="color:#969896">}</span>
in<span style="color:#969896">.</span><span style="color:#795da3">close</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
proc<span style="color:#969896">.</span><span style="color:#795da3">waitFor</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
<span style="color:#969896">}</span> <span style="color:#a71d5d">catch</span> <span style="color:#969896">(</span>IOException e<span style="color:#969896">)</span> <span style="color:#969896">{</span>
e<span style="color:#969896">.</span><span style="color:#795da3">printStackTrace</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
<span style="color:#969896">}</span> <span style="color:#a71d5d">catch</span> <span style="color:#969896">(</span>InterruptedException e<span style="color:#969896">)</span> <span style="color:#969896">{</span>
e<span style="color:#969896">.</span><span style="color:#795da3">printStackTrace</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
<span style="color:#969896">}</span>
<span style="color:#969896">}</span>
<span style="color:#969896">}</span>
</code></span>
输出的结果如下图所示:
可以看到运行成功了,但有的朋友可能会问了,怎么在python程序中函数传递参数并执行出结果,下面我就举一例来说明一下。
先写一个python的程序,代码如下:
<span style="color:#000000"><code class="language-python"><span style="color:#a71d5d">import</span> sys
<span style="color:#a71d5d">def</span> <span style="color:#795da3">func</span><span style="color:#969896">(</span>a<span style="color:#969896">,</span>b<span style="color:#969896">)</span><span style="color:#969896">:</span>
<span style="color:#a71d5d">return</span> <span style="color:#969896">(</span>a<span style="color:#df5000">+</span>b<span style="color:#969896">)</span>
<span style="color:#a71d5d">if</span> __name__ <span style="color:#df5000">==</span> <span style="color:#df5000">'__main__'</span><span style="color:#969896">:</span>
a <span style="color:#df5000">=</span> <span style="color:#969896">[</span><span style="color:#969896">]</span>
<span style="color:#a71d5d">for</span> i <span style="color:#a71d5d">in</span> <span style="color:#df5000">range</span><span style="color:#969896">(</span><span style="color:#c76b29">1</span><span style="color:#969896">,</span> <span style="color:#df5000">len</span><span style="color:#969896">(</span>sys<span style="color:#969896">.</span>argv<span style="color:#969896">)</span><span style="color:#969896">)</span><span style="color:#969896">:</span>
a<span style="color:#969896">.</span>append<span style="color:#969896">(</span><span style="color:#969896">(</span><span style="color:#df5000">int</span><span style="color:#969896">(</span>sys<span style="color:#969896">.</span>argv<span style="color:#969896">[</span>i<span style="color:#969896">]</span><span style="color:#969896">)</span><span style="color:#969896">)</span><span style="color:#969896">)</span>
<span style="color:#a71d5d">print</span><span style="color:#969896">(</span>func<span style="color:#969896">(</span>a<span style="color:#969896">[</span><span style="color:#c76b29">0</span><span style="color:#969896">]</span><span style="color:#969896">,</span>a<span style="color:#969896">[</span><span style="color:#c76b29">1</span><span style="color:#969896">]</span><span style="color:#969896">)</span><span style="color:#969896">)</span>
</code></span>
其中sys.argv用于获取参数url1,url2等。而sys.argv[0]代表python程序名,所以列表从1开始读取参数。
以上代码实现一个两个数做加法的程序,下面看看在java中怎么传递函数参数,代码如下:
<span style="color:#000000"><code class="language-java"><span style="color:#a71d5d">int</span> a <span style="color:#df5000">=</span> <span style="color:#c76b29">18</span><span style="color:#969896">;</span>
<span style="color:#a71d5d">int</span> b <span style="color:#df5000">=</span> <span style="color:#c76b29">23</span><span style="color:#969896">;</span>
<span style="color:#a71d5d">try</span> <span style="color:#969896">{</span>
String<span style="color:#969896">[</span><span style="color:#969896">]</span> args <span style="color:#df5000">=</span> <span style="color:#a71d5d">new</span> String<span style="color:#969896">[</span><span style="color:#969896">]</span> <span style="color:#969896">{</span> <span style="color:#df5000">"python"</span><span style="color:#969896">,</span> <span style="color:#df5000">"D:\\demo2.py"</span><span style="color:#969896">,</span> String<span style="color:#969896">.</span><span style="color:#795da3">valueOf</span><span style="color:#969896">(</span>a<span style="color:#969896">)</span><span style="color:#969896">,</span> String<span style="color:#969896">.</span><span style="color:#795da3">valueOf</span><span style="color:#969896">(</span>b<span style="color:#969896">)</span> <span style="color:#969896">}</span><span style="color:#969896">;</span>
Process proc <span style="color:#df5000">=</span> Runtime<span style="color:#969896">.</span><span style="color:#795da3">getRuntime</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">.</span><span style="color:#795da3">exec</span><span style="color:#969896">(</span>args<span style="color:#969896">)</span><span style="color:#969896">;</span><span style="color:#969896">// 执行py文件</span>
BufferedReader in <span style="color:#df5000">=</span> <span style="color:#a71d5d">new</span> BufferedReader<span style="color:#969896">(</span><span style="color:#a71d5d">new</span> InputStreamReader<span style="color:#969896">(</span>proc<span style="color:#969896">.</span><span style="color:#795da3">getInputStream</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">)</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
String line <span style="color:#df5000">=</span> null<span style="color:#969896">;</span>
<span style="color:#a71d5d">while</span> <span style="color:#969896">(</span><span style="color:#969896">(</span>line <span style="color:#df5000">=</span> in<span style="color:#969896">.</span><span style="color:#795da3">readLine</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">)</span> <span style="color:#df5000">!=</span> null<span style="color:#969896">)</span> <span style="color:#969896">{</span>
System<span style="color:#969896">.</span>out<span style="color:#969896">.</span><span style="color:#795da3">println</span><span style="color:#969896">(</span>line<span style="color:#969896">)</span><span style="color:#969896">;</span>
<span style="color:#969896">}</span>
in<span style="color:#969896">.</span><span style="color:#795da3">close</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
proc<span style="color:#969896">.</span><span style="color:#795da3">waitFor</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
<span style="color:#969896">}</span> <span style="color:#a71d5d">catch</span> <span style="color:#969896">(</span>IOException e<span style="color:#969896">)</span> <span style="color:#969896">{</span>
e<span style="color:#969896">.</span><span style="color:#795da3">printStackTrace</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
<span style="color:#969896">}</span> <span style="color:#a71d5d">catch</span> <span style="color:#969896">(</span>InterruptedException e<span style="color:#969896">)</span> <span style="color:#969896">{</span>
e<span style="color:#969896">.</span><span style="color:#795da3">printStackTrace</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
<span style="color:#969896">}</span>
</code></span>
其中args是String[] { “python”,path,url1,url2 }; ,path是python程序所在的路径,url1是参数1,url2是参数2,以此类推。
最后结果如图所示:
OK,成功了。
总结: 以上就是Java调用python程序的所有方法总结,推荐使用第三种方法,比前两种方便很多。最后,欢迎大家转载,转载请注明出处http://blog.csdn.net/it_xiao_bai/article/details/79074988