您的位置:首页 > 编程语言 > Java开发

java程序调用bat,sh文件

2013-08-22 16:13 162 查看
   //bat文件

    excuteCommand("cmd /k start call "+Path+"/abc.bat");//执行

    private void excuteCommand(String command)

    {

        Runtime r = Runtime.getRuntime();

        Process p;

            try {

                p = r.exec(command);

                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

                String inline;

                while ((inline = br.readLine()) != null) {

                    System.out.println(inline);

                }

                br.close();

            } catch (Exception e) {

                e.printStackTrace();

            }

    }

  //sh文件 

   excuteCommandLinux("chmod -R 777 "+Path+"/projectUpgrade.sh");//授权

    excuteCommandLinux("sh "+Path+"/projectUpgrade.sh > /root/make_log.txt");//执行

    private void excuteCommandLinux(String command)

    {

    

        try {

            String[] commands = {"/bin/sh", "-c", command};

            ProcessBuilder builder = new ProcessBuilder(commands);

        

            builder.redirectErrorStream(true);

            

            Process process = builder.start();

            InputStream in=process.getInputStream();

            byte[] re=new byte[1024];

            

            while (in.read(re)!= -1) {

            

                System.out.println(new String(re));

            }

            

            in.close();

        

        } catch (Exception ex) {

            ex.printStackTrace();

        }

    }

以上方法已经过测试通过,阅读者有不理解之处可以留言
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javashbatshell