【操作ノート】Java呼び出しPython

948 ワード

Javaコード
package com.wd.pydjc.pred.model;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class testPython {
    public static void main(String[] args){
        String [] arguments = new String[]{"python","/Users/xxx/Documents/graduate/pythonFolder/dealData.py"};
        try{
            Process process = Runtime.getRuntime().exec(arguments);
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
            String line = null;
            while ((line = in.readLine())!= null){
                System.out.println(line);
            }
            in.close();
            int re = process.waitFor();
            System.out.println(re);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

Jythonがもたらす3番目のライブラリの呼び出しの問題を解決するには、ファイルを使用するときに絶対パスを使用する必要があります.そうしないと呼び出しに失敗し、1に戻ることに注意してください.