當前位置:首頁 » 編程語言 » java運行python腳本

java運行python腳本

發布時間: 2023-07-14 02:51:35

java調python

很多朋友都想知道java怎麼調python?下面就一起來了解一下吧~

java調python主要有兩種方法:1.使用Runtime.getRuntime()執行腳本文件;2. 將python腳本寫成進程為java提供服務,下面是具體的方法介紹:

第一種:使用Runtime.getRuntime()執行腳本文件

先建立python腳本文件 demo.py
import numpy as np a = np.arange(12).reshape(3,4)print(a)
java調用python程序並輸出該結果
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Demo {     public static void main(String[] args) {         // TODO Auto-generated method stub         Process proc;         try {             proc = Runtime.getRuntime().exec("python D:\\demo.py");// 執行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();         }      }}
如若向python程序中函數傳遞參數並執行出結果,下面就舉一例來說明一下。
同樣建立python腳本文件demo2.py
import sys def func(a,b):     return (a+b)if __name__ == '__main__':     a = []     for i in range(1, len(sys.argv)):         a.append((int(sys.argv[i])))     print(func(a[0],a[1]))
其中sys.argv用於獲取參數url1,url2等。而sys.argv[0]代表python程序名,所以列表從1開始讀取參數。
以上代碼實現一個兩個數做加法的程序,下面看看在java中怎麼傳遞函數參數,代碼如下:
int a = 18;int b = 23;try {     String[] args = new String[] { "python", "D:\\demo2.py", String.valueOf(a), String.valueOf(b) };     Process proc = Runtime.getRuntime().exec(args);// 執行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();}
其中args是String[] { 「python」,path,url1,url2 }; ,path是python程序所在的路徑,url1是參數1,url2是參數2,以此類推。

2. 將python腳本寫成進程為java提供服務

python腳本文件如下:
import socketimport sysimport threadingimport numpy as npfrom PIL import Imagedef main():     # 創建伺服器套接字     serversocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)     # 獲取本地主機名稱     host = socket.gethostname()     # 設置一個埠     port = 12345     # 將套接字與本地主機和埠綁定     serversocket.bind((host,port))     # 設置監聽最大連接數     serversocket.listen(5)     # 獲取本地伺服器的連接信息     myaddr = serversocket.getsockname()     print("伺服器地址:%s"%str(myaddr))     # 循環等待接受客戶端信息     while True:         # 獲取一個客戶端連接         clientsocket,addr = serversocket.accept()         print("連接地址:%s" % str(addr))         try:             t = ServerThreading(clientsocket)#為每一個請求開啟一個處理線程             t.start()             pass         except Exception as identifier:             print(identifier)             pass         pass     serversocket.close()     passclass ServerThreading(threading.Thread):     # words = text2vec.load_lexicon()     def __init__(self,clientsocket,recvsize=1024*1024,encoding="utf-8"):         threading.Thread.__init__(self)         self._socket = clientsocket         self._recvsize = recvsize         self._encoding = encoding        pass     def run(self):         print("開啟線程.....")         try:             #接受數據             msg = ''             while True:                 # 讀取recvsize個位元組                 rec = self._socket.recv(self._recvsize)                 # 解碼                 msg += rec.decode(self._encoding)                 # 文本接受是否完畢,因為python socket不能自己判斷接收數據是否完畢,                 # 所以需要自定義協議標志數據接受完畢                 if msg.strip().endswith('over'):                     msg=msg[:-4]                     break                         sendmsg = Image.open(msg)             # 發送數據             self._socket.send(("%s"%sendmsg).encode(self._encoding))             pass         except Exception as identifier:             self._socket.send("500".encode(self._encoding))             print(identifier)             pass         finally:             self._socket.close()          print("任務結束.....")                  pass     def __del__(self):         passif __name__ == "__main__":     main()
在java代碼中訪問python進程的代碼: package hello;import java.lang.System;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.InetAddress;import java.net.Socket;import java.io.OutputStream;import java.io.PrintStream;import java.io.InputStream;public class hello {     public static void main(String[] args){         //System.out.println("Hello World!");         // TODO Auto-generated method stub         try {             InetAddress addr = InetAddress.getLocalHost();             String host=addr.getHostName();             //String ip=addr.getHostAddress().toString(); //獲取本機ip             //log.info("調用遠程介面:host=>"+ip+",port=>"+12345);             // 初始化套接字,設置訪問服務的主機和進程埠號,HOST是訪問python進程的主機名稱,可以是IP地址或者域名,PORT是python進程綁定的埠號             Socket socket = new Socket(host,12345);             // 獲取輸出流對象             OutputStream os = socket.getOutputStream();             PrintStream out = new PrintStream(os);             // 發送內容             out.print( "F:\\xxx\\0000.jpg");             // 告訴服務進程,內容發送完畢,可以開始處理             out.print("over");             // 獲取服務進程的輸入流             InputStream is = socket.getInputStream();             BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));             String tmp = null;             StringBuilder sb = new StringBuilder();             // 讀取內容             while((tmp=br.readLine())!=null)                 sb.append(tmp).append('\n');             System.out.print(sb);             // 解析結果             //JSONArray res = JSON.parseArray(sb.toString());         } catch (IOException e) {             e.printStackTrace();         }finally {             try {if(socket!=null) socket.close();} catch (IOException e) {}             System.out.print("遠程介面調用結束.");         }       }}

⑵ java運行python腳本時,報錯Exception in thread "main" SyntaxError: Illegal character in file

本地庫不在默認的路徑,編譯器無法找到導致的,再換eclipse也還會報這個錯誤。。。
首先確定已經安裝opencv,安裝好以後要把opencv-246.jar引入到項目的lib庫中,
opencv-246.jar在opencv的安裝目錄下的/build/java下。

其次就是設置項目的native庫,把opencv_java246.dll文件所在的目錄加入到Native library location中,opencv_java246.dll在opencv安裝目錄下的/build/java/x86或/build/java/x64中,前者是32位系統用的,後者64位。

⑶ 建立java與python的介面,讓java能調用python腳本。請問可以用什麼方式

不建議研究jython。比較簡單的思路是把python腳本完全當做一個外部程序,用shell方式調用它。
首先設計好python腳本的介面,把參數用命令行方式傳入,然後輸出列印出來。示例:

$ python func.py arg1 arg2
result
然後在java里就可以用Runtime來執行shell命令,解析輸出字元串然後得到結果。

⑷ 怎麼使用java運行python腳本

1.直接執行Python腳本代碼
引用 org.python包
1 PythonInterpreter interpreter = new PythonInterpreter();
2 interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); ///執行python腳本


2. 執行python .py文件
1 PythonInterpreter interpreter = new PythonInterpreter();
2 InputStream filepy = new FileInputStream("D:\\demo.py");
3 interpreter.execfile(filepy); ///執行python py文件
4 filepy.close();


3. 使用Runtime.getRuntime()執行腳本文件
這種方式和.net下面調用cmd執行命令的方式類似。如果執行的python腳本有引用第三方包的,建議使用此種方式。使用上面兩種方式會報錯java ImportError: No mole named arcpy。
1 Process proc = Runtime.getRuntime().exec("python D:\\demo.py");
2 proc.waitFor();

⑸ 用java執行python

1.直接執行Python腳本代碼
引用 org.python包
1 PythonInterpreter interpreter = new PythonInterpreter();
2 interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); ///執行python腳本

2. 執行python .py文件
1 PythonInterpreter interpreter = new PythonInterpreter();
2 InputStream filepy = new FileInputStream("D:\\demo.py");
3 interpreter.execfile(filepy); ///執行python py文件
4 filepy.close();

3. 使用Runtime.getRuntime()執行腳本文件
這種方式和.net下面調用cmd執行命令的方式類似。如果執行的python腳本有引用第三方包的,建議使用此種方式。使用上面兩種方式會報錯java ImportError: No mole named arcpy。
1 Process proc = Runtime.getRuntime().exec("python D:\\demo.py");
2 proc.waitFor();

⑹ 如何在java工程里運行一個python腳本

可以使用jython


方法參考如下

PythonInterpreter interpreter = new PythonInterpreter();

interpreter.execfile("uar/local/xxx.py");

//pyFunction :py中方法名

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

Integer a = 1

Integer b = 2

// py中方法傳參

PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));

System.out.println("anwser = " + pyobj.toString());

⑺ 如何在Java中調用Python代碼

Jython(原JPython),是一個用Java語言寫的Python解釋器。 在沒有第三方模塊的情況下,通常選擇利用Jython來調用Python代碼, 它是一個開源的JAR包,你可以到官網下載 一個HelloPython程序 importorg.python.util.PythonInterpreter; publicclassHelloPython{ publicstaticvoidmain(String[]args){ PythonInterpreterinterpreter=newPythonInterpreter(); interpreter.exec("print('hello')"); } } 什麼是PythonInterpreter?它的中文意思即是「Python解釋器」。我們知道Python程序都是通過解釋器來執行的,我們在Java中創建一個「解釋器」對象,模擬Python解釋器的行為,通過exec("Python語句")直接在JVM中執行Python代碼,上面代碼的輸出結果為:hello 在Jvm中執行Python腳本 interpreter.execfile("D:/labs/mytest/hello.py"); 如上,將exec改為execfile就可以了。需要注意的是,這個.py文件不能含有第三方模塊,因為這個「Python腳本」最終還是在JVM環境下執行的,如果有第三方模塊將會報錯:javaImportError:Nomolenamedxxx 僅在Java中調用Python編寫的函數 先完成一個hello.py代碼: defhello(): return'Hello' 在Java代碼中調用這個函數: importorg.python.core.PyFunction; importorg.python.core.PyObject; importorg.python.util.PythonInterpreter; publicclassHelloPython{ publicstaticvoidmain(String[]args){ PythonInterpreterinterpreter=newPythonInterpreter(); interpreter.execfile("D:/labs/hello.py"); PyFunctionpyFunction=interpreter.get("hello",PyFunction.class);//第一個參數為期望獲得的函數(變數)的名字,第二個參數為期望返回的對象類型 PyObjectpyObject=pyFunction.__call__();//調用函數 System.out.println(pyObject); } } 上面的代碼執行結果為:Hello 即便只是調用一個函數,也必須先載入這個.py文件,之後再通過Jython包中所定義的類獲取、調用這個函數。 如果函數需要參數,在Java中必須先將參數轉化為對應的「Python類型」,例如: __call__(newPyInteger(a),newPyInteger(b)) a,b的類型為Java中的int型,還有諸如:PyString(Stringstring)、PyList(Iteratoriter)等。 詳細可以參考官方的api文檔。 包含第三方模塊的情況:一個手寫識別程序 這是我和舍友合作寫的一個小程序,完整代碼在這里:,界面上引用了corejava上的一段代碼。Python代碼是舍友寫的,因為在Python程序中使用了第三方的NumPy模塊,導致無法通過Jython執行。下面這個方法純粹是個人思路,沒有深入查資料。核心代碼如下: importjava.io.*; classPyCaller{ privatestaticfinalStringDATA_SWAP="temp.txt"; privatestaticfinalStringPY_URL=System.getProperty("user.dir")+"\test.py"; (Stringpath){ PrintWriterpw=null; try{ pw=newPrintWriter(newFileWriter(newFile(DATA_SWAP))); }catch(IOExceptione){ e.printStackTrace(); } pw.print(path); pw.close(); } publicstaticStringreadAnswer(){ BufferedReaderbr; Stringanswer=null; try{ br=newBufferedReader(newFileReader(newFile(DATA_SWAP))); answer=br.readLine(); }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); } returnanswer; } publicstaticvoidexecPy(){ Processproc=null; try{ proc=Runtime.getRuntime().exec("python"+PY_URL); proc.waitFor(); }catch(IOExceptione){ e.printStackTrace(); }catch(InterruptedExceptione){ e.printStackTrace(); } } //測試碼 publicstaticvoidmain(String[]args)throwsIOException,InterruptedException{ writeImagePath("D:\labs\mytest\test.jpg"); execPy(); System.out.println(readAnswer()); } } 實際上就是通過Java執行一個命令行指令。

熱點內容
新逍客20發動機壓縮比 發布:2025-02-08 17:58:10 瀏覽:115
qq號和密碼我都知道為什麼登不上 發布:2025-02-08 17:52:21 瀏覽:872
寶塔伺服器ip進不去 發布:2025-02-08 17:52:18 瀏覽:382
擔保中介源碼 發布:2025-02-08 17:14:37 瀏覽:412
手機存儲卡速度測試 發布:2025-02-08 17:02:57 瀏覽:25
洪恩編程 發布:2025-02-08 17:02:19 瀏覽:814
linux遠程式控制制 發布:2025-02-08 17:02:16 瀏覽:153
珠心算演算法 發布:2025-02-08 17:00:37 瀏覽:919
動態ip可以做伺服器么 發布:2025-02-08 17:00:33 瀏覽:220
oracle定義存儲過程 發布:2025-02-08 16:54:35 瀏覽:151