Java调用执行python脚本

  public static void main(String[] args) throws IOException, InterruptedException {
		String[] para=new String[] {"python","C:\\Users\\asus\\Desktop\\java_invoked.py"};
        PythonInvoke.invokePy(para);
	}
	
	
	/**
     * java项目调用python脚本 
     */
    public static void invokePy(String[] para) {
        System.out.println("执行....");
        try {
        	Process process = Runtime.getRuntime().exec(para);
        	
        	BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
        	
        	String line = null;
        	while((line = reader.readLine()) != null) {
        		System.out.println(line);
        	}
        	reader.close();
        	process.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("结束....");
    }

  

posted @ 2020-05-29 17:29  木之一  阅读(193)  评论(0编辑  收藏  举报