java调用python库pyd_Java调用Python的两种方式
1、前言
在與第三方程序或語言進(jìn)行交互時(shí),需要Java調(diào)用
2、使用Runtime的exec函數(shù)
在使用時(shí)需注意img = sys.argv[1]取下標(biāo)為1的參數(shù)
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()調(diào)用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] 參數(shù)需下標(biāo)為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調(diào)用python
在使用時(shí)需注意img = sys.argv[0]取下標(biāo)為0的參數(shù)
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調(diào)用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");
// 第一個(gè)參數(shù)為期望獲得的函數(shù)(變量)的名字,第二個(gè)參數(shù)為期望返回的對象類型
pyFunction = interpreter.get("fun", PyFunction.class);
}
PyObject pyobj = pyFunction.__call__(params);
return pyobj.asString();
}
/* python 代碼
img = sys.argv[0] Jython調(diào)用參數(shù)需下標(biāo)為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)
*/
}
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的java调用python库pyd_Java调用Python的两种方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联想 Geek Pro G5000 游戏
- 下一篇: 优派推出 VX2758 显示器:27 英