如题

解决方案 »

  1.   

    java.getRuntime().exec(cmd);
    该方法就可以的
    代码如下:
    //调用
    Process p=Runtime.getRuntime().exec("/bin/bash ./make_package_rtx.sh ",null,new File(Main.destexepath));
    new ControlThread(p.getErrorStream(),"ERROR","make_package_rtx.sh").start();
    new ControlThread(p.getInputStream(),"INFO","make_package_rtx.sh").start(); int status = p.waitFor();
    if(status == 0 )
    {
    RecordLog.logtool(Main.file,Main.type,"INFO", "end to build .");
    }
    else
    {
    RecordLog.logtool(Main.file,Main.type,"ERROR", "failed to build .");
    throw new Exception("CallMakePackage.makepackage : ./*.sh or *.exe or *.bat execute failed . ");
    }
    //要单独的线程来监控
    public class ControlThread extends Thread {

        InputStream is;
        String type;
        String shell;    /**
         * 这个方法是个构造函数,\r\n
         * 主要是初始化一些变量 \r\n
         * @param is 输入流 主要有俩种 普通的输入流,错误流,
         * @param type 传进来的是日志的表示 是error ,warning ,info等。
         * @param shell 这个就是执行的脚本的名称
         * @return no 没有返回值
         * @author 刘欢
         */
        ControlThread(InputStream is, String type,String shell)
        {
            this.is = is;
            this.type = type;
            this.shell= shell;
        }
        
        /**
         * 这个只是重写了Thread 的 run 方法 。 \r\n
         * 在这个方法中,将传进来的流读出来, 并写进日志文件中。\r\n
         * @param no parameter
         * @return no
         * @author 刘欢
         */
        public void run()
        {
            try
            {
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line=null;
                while ( (line = br.readLine()) != null)
                 RecordLog.logtool(Main.file,Main.type,type, shell+" :"+line);;    
                } catch (IOException ioe)
                  {
                    ioe.printStackTrace();  
                  }
        }}
      

  2.   


    public static void main(String[] args) throws Exception {
    // changeIp();

    runtime.exec("perl D:/workOfEdit/PerlWork/bell.pl");

    // InputStream inputStream = runtime.exec(
    // "perl -e \"print \"HelloWorld!\";\" ").getInputStream();
    InputStream inputStream = runtime.exec(
    "perl D:/workOfEdit/PerlWork/HelloWorld.pl").getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(
    inputStream));
    String str = "";
    while ((str = reader.readLine()) != null) {
    System.out.println(str);
    }
    reader.close();
    }