詳解java調(diào)用python的幾種用法(看這篇就夠了)
java調(diào)用python的幾種用法如下:
- 在java類中直接執(zhí)行python語(yǔ)句
- 在java類中直接調(diào)用本地python腳本
- 使用Runtime.getRuntime()執(zhí)行python腳本文件(推薦)
- 調(diào)用python腳本中的函數(shù)
準(zhǔn)備工作:
創(chuàng)建maven工程,結(jié)構(gòu)如下:

到官網(wǎng)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類中直接執(zhí)行python語(yǔ)句
創(chuàng)建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;");
}
}
輸出結(jié)果如下:

出現(xiàn)的console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.并不是錯(cuò)誤,而是兼容所導(dǎo)致,解決方法如下:



2.在java中直接調(diào)用python腳本
在本地的D盤創(chuàng)建一個(gè)python腳本,文件名字為javaPythonFile.py,文件內(nèi)容如下:
a = 1 b = 2 print (a + b)
創(chuàng)建JavaPythonFile.java類,內(nèi)容如下:
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");
}
}
輸出結(jié)果如下:

3.使用Runtime.getRuntime()執(zhí)行python腳本文件,推薦使用
在本地的D盤創(chuàng)建一個(gè)python腳本,文件名字為Runtime.py,文件內(nèi)容如下:
print('RuntimeDemo')
創(chuàng)建RuntimeFunction.java類,內(nèi)容如下:
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();
}
}
}
運(yùn)行結(jié)果如下:

4.調(diào)用python腳本中的函數(shù)
在本地的D盤創(chuàng)建一個(gè)python腳本,文件名字為add.py,文件內(nèi)容如下:
def add(a,b): return a + b
創(chuàng)建Function.java類,內(nèi)容如下:
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");
// 第一個(gè)參數(shù)為期望獲得的函數(shù)(變量)的名字,第二個(gè)參數(shù)為期望返回的對(duì)象類型
PyFunction pyFunction = interpreter.get("add", PyFunction.class);
int a = 5, b = 10;
//調(diào)用函數(shù),如果函數(shù)需要參數(shù),在Java中必須先將參數(shù)轉(zhuǎn)化為對(duì)應(yīng)的“Python類型”
PyObject pyobj = pyFunction.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("the anwser is: " + pyobj);
}
}
運(yùn)行結(jié)果如下:

到此這篇關(guān)于詳解java調(diào)用python的幾種用法(看這篇就夠了)的文章就介紹到這了,更多相關(guān)java調(diào)用python內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python pygame實(shí)現(xiàn)五子棋小游戲
這篇文章主要為大家詳細(xì)介紹了python pygame實(shí)現(xiàn)五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
python GUI庫(kù)圖形界面開發(fā)之PyQt5信號(hào)與槽的高級(jí)使用技巧(自定義信號(hào)與槽)詳解與實(shí)例
這篇文章主要介紹了python GUI庫(kù)圖形界面開發(fā)之PyQt5信號(hào)與槽的高級(jí)知識(shí)(自定義信號(hào)與槽)詳解與實(shí)例,需要的朋友可以參考下2020-03-03
對(duì)DJango視圖(views)和模版(templates)的使用詳解
今天小編就為大家分享一篇對(duì)DJango視圖(views)和模版(templates)的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
python實(shí)現(xiàn)監(jiān)控阿里云賬戶余額功能
這篇文章主要介紹了python實(shí)現(xiàn)監(jiān)控阿里云賬戶余額功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
Python腳本打包成可執(zhí)行文件過(guò)程解析
這篇文章主要介紹了Python腳本打包成可執(zhí)行文件過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
python 使用pygame工具包實(shí)現(xiàn)貪吃蛇游戲(多彩版)
這篇文章主要介紹了python 使用pygame工具包實(shí)現(xiàn)貪吃蛇游戲,本篇給大家分享的是一個(gè)多彩版,通過(guò)實(shí)例代碼給大家講解,非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Python可執(zhí)行文件反編譯教程(exe轉(zhuǎn)py)
python的便利性,使得如今許多軟件開發(fā)者、黑客都開始使用python打包成exe的方式進(jìn)行程序的發(fā)布,那么Python如何反編譯可執(zhí)行文件,本文就來(lái)介紹一下,感興趣的可以了解一下2021-12-12

