一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

java怎么运行getRuntime?java使用Runtime.getRuntime来执行python脚本

时间:2017-02-16 编辑:简简单单 来源:一聚教程网

下面是直接使用Java的一个函数,Runtime类来进行Python脚本的调用。下面对对应的java程序

  1.  代码如下复制代码
    1. import java.io.IOException;  

    2.   

    3.   

    4. public class TestJava {  

    5.     public static void main(String[] args) throws IOException{  

    6.         // 使用用Ping命令   

    7.   

    8.         //Process ee = Runtime.getRuntime().exec(   

    9.         //"cmd.exe /c start ping 10.5.2.19");  

    10.         /* 

    11.         try{ 

    12.             Runtime rt = Runtime.getRuntime(); 

    13.             //System.out.println("start!"); 

    14.             Process proc = Runtime.getRuntime().exec("cmdpython c:\\Python32\\program\\outfile.py"); 

    15.             proc.waitFor(); //waiting the completion of the command 

    16.              

    17.             //System.out.println("end"); 

    18.         }catch (Exception e){ 

    19.             e.printStackTrace(); 

    20.         } 

    21.         */  

    22.         //ok 这个是可以使用的   

    23.         try{  

    24.             Runtime rt = Runtime.getRuntime();  

    25.             System.out.println("start");  

    26.             Process proc = Runtime.getRuntime().exec("python outfile.py");  

    27.             proc.waitFor();  

    28.             System.out.println("end");  

    29.             } catch (Exception e){  

    30.             e.printStackTrace();  

    31.             }  

    32.   

    33.   

    34.           

    35.   

    36.         }//main  

    37.       

    38.   

    39.   


上面要求outfile.py是在java工程的根目录下的。

热门栏目