java 调用 python代码
java
其中arg是String[] { “python”,path,url1,url2 }; ,path是python程序所在的路径,url1是参数1,url2是参数2,以此类推。
public class Test11 { public static void main(String[] args) { int a = 18; int b = 23; try { String[] arg = new String[] { "python", "D:\\demo2.py", String.valueOf(a), String.valueOf(b) }; Process proc = Runtime.getRuntime().exec(arg);// 执行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
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])) 或者 # coding=utf-8 from sys import argv a = argv[1] b = argv[2] res = int(a) + int(b) print(res)
参考 https://blog.csdn.net/qq_26591517/article/details/80441540