I run egrep using Java Runtime.exec()
String command = "egrep \'(Success|Loading\\.\\.\\.|Loaded : READY|Found a running instance)\' "+ instance.getPath() + "/log";
Runtime.getRuntime().exec(command);
The stdout is always null and stderr shows “egrep: Unmatched ( or (“. but when I copy the command to shell and run, it returns the correct value.
The solution is pretty simple:
(Success|Loading\\.\\.\\.|Loadedis not a valid regex.You can’t protect white space with quotes when using
Process.exec(String). Always use the versions ofexec()that take an array or, even better, useProcessBuilder.That way, you can pass each argument as a single Java String and spaces and other special characters won’t create any problems.