欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解java调用python的几种用法(看这篇就够了)

程序员文章站 2022-06-15 13:19:35
java调用python的几种用法如下: 在java类中直接执行python语句 在java类中直接调用本地python脚本 使用runtime.getruntime()执行python脚本文件...

java调用python的几种用法如下:

  • 在java类中直接执行python语句
  • 在java类中直接调用本地python脚本
  • 使用runtime.getruntime()执行python脚本文件(推荐)
  • 调用python脚本中的函数

准备工作:

创建maven工程,结构如下:

详解java调用python的几种用法(看这篇就够了)

到官网下载jython的jar包或者在maven的pom.xml文件中加入如下代码:

<dependency>
  <groupid>org.python</groupid>
  <artifactid>jython-standalone</artifactid>
  <version>2.7.0</version>
</dependency>

1.在java类中直接执行python语句

创建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;");
  }

}

输出结果如下:

详解java调用python的几种用法(看这篇就够了)

出现的console: failed to install '': java.nio.charset.unsupportedcharsetexception: cp0.并不是错误,而是兼容所导致,解决方法如下:

详解java调用python的几种用法(看这篇就够了)

详解java调用python的几种用法(看这篇就够了)

详解java调用python的几种用法(看这篇就够了)

2.在java中直接调用python脚本

在本地的d盘创建一个python脚本,文件名字为javapythonfile.py,文件内容如下:

a = 1
b = 2
print (a + b)

创建javapythonfile.java类,内容如下:

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

输出结果如下:

详解java调用python的几种用法(看这篇就够了)

3.使用runtime.getruntime()执行python脚本文件,推荐使用

在本地的d盘创建一个python脚本,文件名字为runtime.py,文件内容如下:

print('runtimedemo')

创建runtimefunction.java类,内容如下:

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

运行结果如下:

详解java调用python的几种用法(看这篇就够了)

4.调用python脚本中的函数

在本地的d盘创建一个python脚本,文件名字为add.py,文件内容如下:

def add(a,b):
  return a + b

创建function.java类,内容如下:

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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!