-
Runtime.getRuntime().exec() 특수문자가 들어갈때 유의점Java/Script 2019. 3. 8. 17:26728x90
코드설명 : Runtime.getRuntime().exec() 특수문자가 들어갈때 유의점
12345678910111213141516171819202122232425262728293031package dao;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class RunTask {public void execute() throws IOException{BufferedReader br = null;Process process = null;Process process1 = null;StringBuffer sb = new StringBuffer();process = Runtime.getRuntime().exec("tasklist /nh | sort /r /+64");process1 = Runtime.getRuntime().exec("cmd.exe /C " + "tasklist /nh | sort /r /+64");br = new BufferedReader(new InputStreamReader(process.getInputStream()));while(br.readLine() != null) {System.out.println(br.readLine());}if (br != null) br.close();System.out.println(process.exitValue()); // ExitValue = 1System.out.println(process1.exitValue()); // ExitValue = 0}}cs process 와 process1 을 비교하면 두 프로세스 모두 "tasklist /nh | sort /r /+64" 라는 커맨드를 실행하는데
process1 은 cmd.exe /C 가 실행 command 앞에 붙어있고
process 는 실패, process1은 성공인 결과를 얻을 수 있다.
이유는 중간에 " | "의 특수문자 때문이였는데 이와같이 특수문자가 들어가는 command 실행은
실행 command 앞에 cmd.exe /C 옵션을 주면 해결된다.
728x90