程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Pure record -- Java calls Python script

編輯:Python
  • Be sure to choose the right one python compiler , Because the script may contain a third-party library , An environment error will cause the call to fail

  • As if to call later python A small template for

package com.neusoft;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class demo {

public static void main(String[] args) {

try {

String res = getRes();
System.out.println(res);
} catch (Exception e) {

System.out.println(e);
}
}
// call python Script functions
public static String getRes() {

String result = "";
try {

Process process;
process = Runtime.getRuntime().exec("D:/mysoft/anaconda/conda/envs/tf2py37/python.exe E:/javademos/serverlet1/Python/animefaces.py");
BufferedReader ir = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = ir.readLine()) != null) {

result = line;
}
ir.close();
process.waitFor();
} catch (IOException e) {

System.out.println(" call python Script failed !");
} catch (InterruptedException e) {

e.printStackTrace();
}
return result;
}
}

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved