ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Runtime.getRuntime().exec() 특수문자가 들어갈때 유의점
    Java/Script 2019. 3. 8. 17:26
    728x90

    코드설명 : Runtime.getRuntime().exec() 특수문자가 들어갈때 유의점


    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
    package 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 = 1
     
            System.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

    'Java > Script' 카테고리의 다른 글

    Java에서 정규식 패턴 체크  (0) 2018.06.12
    while문  (0) 2018.02.19
    for문  (0) 2018.02.19
    switch문  (0) 2018.02.19
    if문  (0) 2018.02.19

    댓글

Designed by Tistory.