Back-End/Java
[Java] Java에서 외부 jar 실행 후 출력되는 결과 가져오기
유자맛바나나
2022. 2. 15. 04:50
외부 jar를 실행할 때 실행한 jar에서 Console에 출력하는 결과를 가져와야 할 때가 있다.
Process 객체가 제공하는 InputStream을 이용해 해당 결과를출력할 수 있다.
Code
public class MyProcessTest {
public static void main(String[] args) {
try
{
Runtime r = Runtime.getRuntime();
Process p = r.exec("java -jar TestJar.jar");
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while (true)
{
String s = br.readLine();
if (s == null)
break;
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
}