美文网首页
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外部程序

  • 外部程序

    反引号` 反引号用于将所附文本作为命令执行。运行的命令的输出可以赋值给变量。

  • NODEJS硬实战笔记(多进程)

    利用NODE整合外部应用程序 执行外部应用程序 execFile:执行外部程序,并且需要提供一组参数,以及一个在进...

  • go语言得到子程序的返回值

    go语言调用外部程序,并获取外部程序的返回值。 例子1: 程序返回0 运行结果 例子2:程序返回非零 运行结果 这...

  • 理解什么是上下文

    每一段程序都有很多外部变量。只有像Add这种简单的函数才是没有外部变量的。一旦你的一段程序有了外部变量,这段程序就...

  • golang读取外部命令的屏幕输出

    golang调用外部程序,如何读取外部程序的stdout/stderr输出: 例子1 读取标准输出 运行结果: c...

  • php危险函数

    exec() 允许执行一个外部程序(如shell或cmd命令等) passthru() 执行外部程序并且显示原始输...

  • awk命令详解

    语法格式 内置变量 外部变量传给awk 打开外部文件 调用外部应用程序 其他示例

  • python30-python调用外部程序

    本篇文章主要讲的是python调用外部程序基本操作。python被称为“胶水语言”,可以粘合各种外部程序和各种语言...

  • php执行外部程序函数

    exec():执行一个外部程序

网友评论

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

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