Java呼叫Python的兩種方式
阿新 • • 發佈:2020-12-26
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) */ }