日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

java调用python接口详解

發布時間:2023/12/31 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java调用python接口详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 在java類中直接執行python語句
  • 在java類中直接調用本地python腳本
  • 使用Runtime.getRuntime()執行python腳本文件(推薦)
  • 調用python腳本中的函數

準備工作:

創建maven工程,結構如下:

到官網https://www.jython.org/download.html下載Jython的jar包或者在maven的pom.xml文件中加入如下代碼:

1

2

3

4

5

<dependency>

??<groupId>org.python</groupId>

??<artifactId>jython-standalone</artifactId>

??<version>2.7.0</version>

</dependency>

1.在java類中直接執行python語句

創建JavaRunPython.java類:

1

2

3

4

5

6

7

8

9

10

11

12

13

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,文件內容如下:

1

2

3

a = 1

b = 2

print (a + b)

創建JavaPythonFile.java類,內容如下:

1

2

3

4

5

6

7

8

9

10

11

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,文件內容如下:

1

print('RuntimeDemo')

創建RuntimeFunction.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

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,文件內容如下:

1

2

def add(a,b):

??return a + b

創建Function.java類,內容如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

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);

??}

}

運行結果如下:

到此這篇關于詳解java調用python的幾種用法(看這篇就夠了)的文章就介紹到這了,更多相關java調用python內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

總結

以上是生活随笔為你收集整理的java调用python接口详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。