当前位置 博文首页 > 文章内容

    详解java调用python的用法

    作者:shunshunshun18 栏目:未分类 时间:2020-12-17 21:03:41

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    栏目介绍java调用python的用法

    相关免费学习

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

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

    准备工作:

    创建maven工程,结构如下:

    到官网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类中直接执行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;");
      }
    
    }

    输出结果如下:

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

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

    输出结果如下:

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

    运行结果如下:

    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的几种用法(看这篇就够了)的文章就介绍到这了。

    相关免费学习