java程序调用bat,sh文件

   //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();
        }
    }


以上方法已经过测试通过,阅读者有不理解之处可以留言

Guess you like

Origin blog.csdn.net/wnn654321/article/details/10186591