通过Java执行python文件
生活随笔
收集整理的這篇文章主要介紹了
通过Java执行python文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
public static void main(String[] args) {// TODO Auto-generated method stub
Process proc;try {String[] args1 = new String[] { "python.exe", "python.py","test"};//python.exe處為你系統(tǒng)中python的安裝位置;python.py為想要執(zhí)行的python文件位置;test為想要傳的參數(shù)//proc = Runtime.getRuntime().exec("python.exe python.py ");// 執(zhí)行py文件 不傳參數(shù)proc=Runtime.getRuntime().exec(args1);//用輸入輸出流來截取結(jié)果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.py文件
import sysprint(sys.argv[0]) //輸出為python文件的路徑 print(sys.argv[1]) //輸出為傳入的第一個參數(shù)test?
轉(zhuǎn)載于:https://www.cnblogs.com/sakura--/p/11430000.html
總結(jié)
以上是生活随笔為你收集整理的通过Java执行python文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: openresty开发系列17--lua
- 下一篇: openresty开发系列18--lua