配置环境:
在Maven中输入以下代码并记得刷新
<dependency>java <groupId>org.python</groupId> <artifactId>jython-standalone</artifactId> <version>2.7.0</version> </dependency>
代码:
try { // TODO 此处改为你python文件的绝对路径 String[] args1 = new String[] { "python", "D:\\\\testpy\\tese.py" }; Process proc = Runtime.getRuntime().exec(args1);// 执行py文件 //调用字符集BufferedReader流读取文件内容,如果输出到控制台用GBK,其他地方用UTF-8 BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"GBK")); String line = null; while ((line = in.readLine()) != null) { //输出结果 System.out.println(line); } in.close(); proc.waitFor(); } catch (IOException | InterruptedException e) { e.printStackTrace(); }