java执行python文件的方法

 1 public static void A(String pythonPath, String pyPath){
 2   Process proc;
 3   String[] arg = new String[] { "python3", "后缀为.py的文件", "参数1", "参数2", "参数3" }; // 参数可继续增加,但是有长度限制
 4   try {
 5     proc = Runtime.getRuntime().exec(arg);
 6     System.out.println("执行成功,开始输出参数:");
 7     BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
 8     String line = null;
 9     while ((line = in.readLine()) != null) {
10       System.out.println(line);
11     }
12     in.close();
13     proc.waitFor();
14   } catch (IOException e) {
15     e.printStackTrace();
16   } catch (InterruptedException e) {
17     e.printStackTrace();
18   }
19 }

 

posted @ 2020-03-19 20:26  老孙家人  阅读(703)  评论(0)    收藏  举报