美文网首页
Runtime.getRuntime.exec外部程序

Runtime.getRuntime.exec外部程序

作者: Ed_Lannister | 来源:发表于2018-03-22 18:11 被阅读65次
    import java.util.*;  
    import java.io.*;  
    //output process class
    class StreamGobbler extends Thread  
    {  
        InputStream is;  
        String type;  
        OutputStream os;  
          
        StreamGobbler(InputStream is, String type)  
        {  
            this(is, type, null);  
        }  
        StreamGobbler(InputStream is, String type, OutputStream redirect)  
        {  
            this.is = is;  
            this.type = type;  
            this.os = redirect;  
        }  
          
        public void run()  
        {  
            try  
            {  
                PrintWriter pw = null;  
                if (os != null)  
                    pw = new PrintWriter(os);  
                      
                InputStreamReader isr = new InputStreamReader(is);  
                BufferedReader br = new BufferedReader(isr);  
                String line=null;  
                while ( (line = br.readLine()) != null)  
                {  
                    if (pw != null)  
                        pw.println(line);  
                    System.out.println(type + ">" + line);      
                }  
                if (pw != null)  
                    pw.flush();  
            } catch (IOException ioe)  
                {  
                ioe.printStackTrace();    
                }  
        }  
    }
    
    //method
    public static void updateEngineClient(boolean full) {
            Runtime runtime = Runtime.getRuntime();
            Process process = null;
            System.out.println("Edward start to update");
            String cmd = null;
            BufferedReader reader = null;
            StringBuilder builder = new StringBuilder();
            StringBuilder builderCmd = new StringBuilder();///////////////
            File cmdFile= new File("/data/ota_package/updatezipinfo.txt");
            //get cmd here
            try {
                System.out.println("Edward Updatezipinfo.txt has been found");
                reader = new BufferedReader(new FileReader(cmdFile));
                for(int i=0; i < 5; i++){
                    cmd = reader.readLine();
                    //System.out.println("Edward get buildercmd,cmd is not null:" + cmd);
                    builderCmd.append(cmd);
                    builderCmd.append("\n");
                    System.out.println("Edward final buildercmd is i is  :" + i + "buildercmd is \n" + builderCmd.toString());
                }
                //System.out.println("Edward final buildercmd is1 :" + builderCmd.toString());
            } catch (Exception e) {
                        e.printStackTrace();
            }
            System.out.println("Edward final buildercmd is2 :" + builderCmd.toString());
            try {
                System.out.println("Edward Begin to exec cmd here and cmd is:" + builderCmd.toString() + ".");
                process = runtime.exec(builderCmd.toString());
                // any error message?  
                        StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERR");              
                  
                    // any output?  
                    StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUT");  
                      
                        // kick them off  
                        errorGobbler.start();  
                        outputGobbler.start();  
                                          
                        // any error???  
                        int exitVal = proc.waitFor();  
                System.out.println(process.exitValue());
            }catch (InterruptedException e) {
                e.printStackTrace();
            }catch (IOException e) {
                e.printStackTrace();
            }finally {
                process.destroy();
            }
        } 
    
    

    相关文章

      网友评论

          本文标题:Runtime.getRuntime.exec外部程序

          本文链接:https://www.haomeiwen.com/subject/oafzqftx.html