java+python

       java中执行python脚本文件

       主要是通过Runtime执行python命令,举例如下:

public class PythonUtil {
	
	private final static String PYTHON_PATH = "python";
	
	public static String getUrl(String name) {
		String classpath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		if(classpath.startsWith("/")) {
			classpath = classpath.substring(1);
		}
		return classpath+PYTHON_PATH+name;
	}
	
	public static void execScript(String file) {
		
		try {
			Process pr = Runtime.getRuntime().exec("python " + file);
		    BufferedReader in = new BufferedReader(
		    		new InputStreamReader(pr.getInputStream())
		    );
		    String line;
		    while ((line = in.readLine()) != null) {
	    		System.out.println(line);
	    	}
	    	in.close();
	    	pr.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}



你可能感兴趣的:(java,python)