下面是直接使用Java的一个函数,Runtime类来进行Python脚本的调用。下面对对应的java程序
代码如下 | 复制代码 |
import java.io.IOException; public class TestJava { public static void main(String[] args) throws IOException{ // 使用用Ping命令 //Process ee = Runtime.getRuntime().exec( //"cmd.exe /c start ping 10.5.2.19"); /* try{ Runtime rt = Runtime.getRuntime(); //System.out.println("start!"); Process proc = Runtime.getRuntime().exec("cmdpython c:\\Python32\\program\\outfile.py"); proc.waitFor(); //waiting the completion of the command //System.out.println("end"); }catch (Exception e){ e.printStackTrace(); } */ //ok 这个是可以使用的 try{ Runtime rt = Runtime.getRuntime(); System.out.println("start"); Process proc = Runtime.getRuntime().exec("python outfile.py"); proc.waitFor(); System.out.println("end"); } catch (Exception e){ e.printStackTrace(); } }//main }
|
上面要求outfile.py是在java工程的根目录下的。