java调用python库pyd_Java调用Python的两种方式

本文介绍了两种在Java中调用Python的方法:1) 使用Runtime.exec(),需要注意参数传递;2) 通过Jython直接执行Python代码,实现更紧密的交互。示例代码展示了如何在定时任务中调用Python脚本并获取返回结果。
摘要由CSDN通过智能技术生成

1、前言

在与第三方程序或语言进行交互时,需要Java调用

2、使用Runtime的exec函数

在使用时需注意img = sys.argv[1]取下标为1的参数

package com;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Timer;

import java.util.TimerTask;

/**

* Java通过Runtime.exec()调用python

*/

public class CallPythonExec {

private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public static void main(String[] args) {

run();

}

public static void run() {

Timer timer = new Timer();

timer.schedule(new TimerTask() {

@Override

public void run() {

String exe = "python";

// String command = "/usr/local/python/videoRec.py";

String command = "D:\\VideoRec\\Python\\videoRec.py";

String param = sdf.format(new Date());

String[] args1 = new String[]{exe, command, param};

System.out.println("result=" + callPython(args1));

}

}, 0, 1000);

}

private static String callPython(String... param) {

String result = "";

Process process = null;

BufferedReader reader = null;

try {

process = Runtime.getRuntime().exec(param);

reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line;

while ((line = reader.readLine()) != null) {

if (!"".equals(line)) {

result = line;

}

}

// System.out.println("waitFor=" + process.waitFor());

} catch (Exception e) {

e.printStackTrace();

} finally {

if (process != null) process.destroyForcibly();

if (reader != null) {

try {

reader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return result;

}

}

/* python 代码

img = sys.argv[1] 参数需下标为1

import sys

def fun(img):

#print("python print="+img)

#print(img)

return img

if __name__ == '__main__':

img = sys.argv[1]

result=fun(img)

print(result)

*/

}

3、使用Jython调用python

在使用时需注意img = sys.argv[0]取下标为0的参数

package com;

import org.python.core.PyFunction;

import org.python.core.PyObject;

import org.python.core.PyString;

import org.python.util.PythonInterpreter;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Timer;

import java.util.TimerTask;

/**

* Java通过Jython调用python

*/

public class CallPythonJython {

private static PyFunction pyFunction = null;

private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public static void main(String[] args) {

run();

}

public static void run() {

Timer timer = new Timer();

timer.schedule(new TimerTask() {

@Override

public void run() {

String result = callPython(new PyString(sdf.format(new Date())));

System.out.println(result);

}

}, 0, 1000);

}

public static String callPython(PyObject... params) {

if (pyFunction == null) {

PythonInterpreter interpreter = new PythonInterpreter();

// interpreter.execfile("/usr/local/python/videoRec.py");

interpreter.execfile("D:\\VideoRec\\Python\\videoRec.py");

// 第一个参数为期望获得的函数(变量)的名字,第二个参数为期望返回的对象类型

pyFunction = interpreter.get("fun", PyFunction.class);

}

PyObject pyobj = pyFunction.__call__(params);

return pyobj.asString();

}

/* python 代码

img = sys.argv[0] Jython调用参数需下标为0

import sys

def fun(img):

#print("python print="+img)

#print(img)

return img

if __name__ == '__main__':

img = sys.argv[0]

result=fun(img)

print(result)

*/

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值