java中调用python程序

1.新建一个maven工程,导入如下依赖

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

2.在java中直接执行python代码片段

import org.python.util.pythoninterpreter;

public class invokepython {

    public static void main(string[] args) {
        pythoninterpreter pythoninterpreter = new pythoninterpreter();
        pythoninterpreter.exec("a='aaa'");
        pythoninterpreter.exec("print(a)");
//        pythoninterpreter.exec("import pandas as pd");
    }
}

通过上面这种方式执行python代码片段,实际上是通过jpython来实现的,这种方式能执行的python代码片段比较有限,都是一些最原始的python命令,很多包不能用,例如执行pythoninterpreter.exec("import pandas as pd"); 都会报错。所以这种方式一般不推荐

3.通过pythoninterpreter类中的execfile()方法来执行一个python脚本文件。

import org.python.util.pythoninterpreter;

public class invokepython {

    public static void main(string[] args) {
        pythoninterpreter pythoninterpreter = new pythoninterpreter();
        pythoninterpreter.execfile("f:\\大学\\大三\\大三下\\工程创新和企业开发\\大作业\\图灵api.py");
    }
}

这种方式和上面的那种方式的本质是一样的,能执行的命令也是很原始的,一般不推荐。

4.通过runtime.getruntime().exec()方法来执行python脚本

原python脚本

import requests
import json
import sys

def chat_by_turing(question):
    url = "http://www.tuling123.com/openapi/api?key=49de46c409c047d19b2ed2285e8775a6&info="
    response = requests.get(url+question)
    result = json.loads(response.text)
    answer = result['text']
    print("小安:",answer)

question = sys.argv[1] ##这个是用来接收外部传进来的参数
chat_by_turing(question)

runtime.getruntime().exec()调用

import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.util.scanner;

public class runtimefunction {
    public static void main(string[] args) {
        process proc;
        string compiler = "e:\\anaconda\\anaconda_install\\python.exe";
//        string program = "f:\\大学\\大三\\大三下\\工程创新和企业开发\\大作业\\图灵api.py";
        string rootpath = "f:\\大学\\大三\\大三下\\机器学习\\课设\\python\\src\\main\\resources\\";
        string program = "图灵api.py";
        try {
            scanner in = new scanner(system.in);
            system.out.print("我:");
            string question = in.nextline();
            string commond = compiler+" "+rootpath+program+" "+question;
            proc = runtime.getruntime().exec(commond);
            bufferedreader reader = new bufferedreader(new inputstreamreader(proc.getinputstream(),"gbk"));
            string line = null;
            while ((line = reader.readline()) != null) {
                system.out.println(line);
            }
            in.close();
            proc.waitfor();

        } catch (ioexception e) {
            e.printstacktrace();
        } catch (interruptedexception e) {
            e.printstacktrace();
        }
    }
}

runtime.getruntime().exec()需要传入一个字符串类型的参数command ,完整语句runtime.getruntime().exec(command)。这条语句是通过自己电脑上的cmd来执行的python程序文件的,因此command的构成如下:
python解释器+” “+python程序文件的绝对路径+” “+需要给python程序文件传入的参数
注意command中的空格比不可少,python脚本里面需要通过sys.argv来接收参数的

通过这种方式来执行python程序,完全取决于你前面使用的python编译器,编译器环境里面有的,就一定能够通过这种方式调用。这种方式比较强大,推荐使用这种方式来执行python程序。

到此这篇关于详解如何在java中调用python程序的文章就介绍到这了,更多相关java中调用python程序内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!