web123456

Java uses ProcessBuilder to execute local commands or scripts

package ExecLocalCommand; import ; import ; public class ProcessBuilderDemo { public void exeLocalCommand(String exportCmd) throws Exception{ ProcessBuilder builder; String charSet; String os = (""); if (().contains("win")) { builder = new ProcessBuilder("cmd", "/c", ()); charSet = "gbk"; } else { builder = new ProcessBuilder("sh", "-c", ()); charSet = "utf-8"; }//Redirecting the error log information to the inputstream is very important, otherwise the error information cannot be crawled(true); Process process = (); BufferedReader br = new BufferedReader(new InputStreamReader((),charSet)); String line = null; while ((line = ()) != null) { line = (); (line); }// Wait for the command execution to complete int code= (); if (code == 0) {//Usually 0 means that the command or script exits normally, but if the script itself returns to the state, it is necessary to judge based on its own state.("success"); } else { ("fail"); } } }