기타
[Linux] 표준 출력(stdout) & 표준 에러(stderr) 읽어오기
phyho
2025. 1. 27. 17:30
표준출력 (stdout) : 정상적인 실행 결과.
표준에러 (stderr) : 에러 메시지.
로컬에서 스크립트(script.sh)를 실행하는 경우.
Process process = Runtime.getRuntime().exec("./script.sh");
표준출력(stdOut) 읽어오기. getInputStream()
try (BufferedReader stdOut = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = stdOut.readLine()) != null) {
System.out.println("STDOUT: " + line);
}
} catch (IOException e) {
e.printStackTrace();
}
표준에러(stdErr) 읽어오기. getErrorStream()
try (BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
String line;
while ((line = stdError.readLine()) != null) {
System.out.println("STDERR: " + line);
}
} catch (IOException e) {
e.printStackTrace();
}