1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| ublic class ProcessUtils {
public static ExecuteMessage runProcessAdnGetMessage(Process runProcess, String opName) { ExecuteMessage executeMessage = new ExecuteMessage(); try { StopWatch stopWatch = new StopWatch(); stopWatch.start(); int exitValue = runProcess.waitFor(); executeMessage.setExitValue(exitValue); if (exitValue == 0) { System.out.println(opName + "成功"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(runProcess.getInputStream())); List<String> outputList = new ArrayList<>(); String compileOutputLine; while ((compileOutputLine = bufferedReader.readLine()) != null) { outputList.add(compileOutputLine); } executeMessage.setMessage(StringUtils.join(outputList, "\n")); } else { System.out.println(opName + "失败" + exitValue);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
List<String> outputList = new ArrayList<>(); String compileOutputLine; while ((compileOutputLine = bufferedReader.readLine()) != null) { outputList.add(compileOutputLine); } executeMessage.setMessage(StringUtils.join(outputList, "\n")); BufferedReader errorBufferedReader = new BufferedReader(new InputStreamReader(runProcess.getErrorStream()));
List<String> errorOutpputList = new ArrayList<>(); String errorCompileOutputLine; while ((errorCompileOutputLine = errorBufferedReader.readLine()) != null) { errorOutpputList.add(errorCompileOutputLine); } executeMessage.setErrorMessage(StringUtils.join(errorOutpputList, "\n")); } stopWatch.stop(); executeMessage.setTime(stopWatch.getLastTaskTimeMillis()); } catch (Exception e) { e.printStackTrace(); } return executeMessage; }
public static ExecuteMessage runInteractProcessAdnGetMessage(Process runProcess, String opName, String args) { ExecuteMessage executeMessage = new ExecuteMessage(); try { OutputStream outputStream = runProcess.getOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); String[] s = args.split(" "); outputStreamWriter.write(StrUtil.join("\n", s) + "\n"); outputStreamWriter.flush();
InputStream inputStream = runProcess.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder compileOutputStringBuilder = new StringBuilder(); String compileOutputLine; while ((compileOutputLine = bufferedReader.readLine()) != null) { compileOutputStringBuilder.append(compileOutputLine); } executeMessage.setMessage(compileOutputStringBuilder.toString()); outputStreamWriter.close(); inputStream.close(); outputStream.close(); runProcess.destroy();
} catch (Exception e) { e.printStackTrace(); } return executeMessage; } }
|