Alee, 네, Runtime.getRuntime(). exec ("file.bat");로 할 수 있습니다. 그리고 당신은 당신이 실행 예를 들어
의 출력을 캡처 할 수 있습니다,이 옵션을 사용할 수 있습니다
이 당신이 출력을 캡처하고 스크립트가 성공적으로 실행 여부를 한 후 결정해야 두 시나리오를 캡처
public static void main(String[] args) {
System.out.println("hello");
try {
Process p = Runtime.getRuntime().exec("./prog.sh");
InputStream in = p.getInputStream();
System.out.println("OUTPUT");
StringBuilder sb = new StringBuilder();
int c;
while((c = in.read()) > 0) {
sb.append((char) c);
}
//here the script finished
String output = sb.toString();
if(output.contains("Exception")) {
System.out.println("script failed");
}
if(p.exitValue() == 0) {
System.out.println("The script run without errors");
} else {
System.out.println("The script failed");
}
} catch (IOException e) {
e.printStackTrace();
}
}
, 또는 스크립트의 종료 상태를 사용할 수 있는지 여부를 나타냅니다.
exitValue 코드는 성공한 경우 0이고 실패한 경우 다른 숫자입니다.
이 출력 내에서 특정 문자열을 검색 할 수 있습니까? 해당 콘솔에서 예외가 발생하여 콘솔을 적절한 종료 코드로 종료 할 수 있습니다. 예외로 인해 프로세스가 완료되지 않았습니다. 내가 어떻게 처리 할 수 있니? –
예, 위의 업데이트를 확인하십시오 – Augusto
Windows에서 완료하려면 exec'd 프로세스의 stdout/stderr 스트림을 사용해야합니다. – maerics