You are not capturing STDERR, so when errors occur you do not receive them from STDOUT (which you are capturing). Try:
CopyBufferedReader input = new BufferedReader(new InputStreamReader(
pr.getErrorStream()));
Answer from hkd93 on Stack OverflowYou are not capturing STDERR, so when errors occur you do not receive them from STDOUT (which you are capturing). Try:
CopyBufferedReader input = new BufferedReader(new InputStreamReader(
pr.getErrorStream()));
well tesseract is external command so you do not need to use it with cmd. Add tesseract to environment variables. Use direct command as :
CopyString[] commands = {"tesseract", imageFilePath, outputFilePath };
Exist status 1 means Incorrect function. See process exit status
Here is the way to go:
Runtime rt = Runtime.getRuntime();
String[] commands = {"system.exe", "-get t"};
Process proc = rt.exec(commands);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));
// Read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// Read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
Read the Javadoc for more details here. ProcessBuilder would be a good choice to use.
A quicker way is this:
public static String execCmd(String cmd) throws java.io.IOException {
java.util.Scanner s = new java.util.Scanner(Runtime.getRuntime().exec(cmd).getInputStream()).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
Which is basically a condensed version of this:
public static String execCmd(String cmd) throws java.io.IOException {
Process proc = Runtime.getRuntime().exec(cmd);
java.io.InputStream is = proc.getInputStream();
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
String val = "";
if (s.hasNext()) {
val = s.next();
}
else {
val = "";
}
return val;
}
I know this question is old but I am posting this answer because I think this may be quicker.
Edit (For Java 7 and above)
Need to close Streams and Scanners. Using AutoCloseable for neat code:
public static String execCmd(String cmd) {
String result = null;
try (InputStream inputStream = Runtime.getRuntime().exec(cmd).getInputStream();
Scanner s = new Scanner(inputStream).useDelimiter("\\A")) {
result = s.hasNext() ? s.next() : null;
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
process.waitFor();
int exitCode = process.exitValue();
if(exitCode == 0) { // success }
else { // failed }
This works, if the Test is designed properly and returns appropriate exit codes (generally, >0 if something went wrong).
If you want to get Tests output/error message to determine what was wrong, you should get proc.getInputStream() (this returns the output stream of the child process), proc.getErrorStream() and read from the input streams in separated threads.
Note that the child process will get blocked if it writes to error/output stream and there are no readers. So reading error/output streams of the process is useful in any cases.
Another option to avoid child blocking is to redirect its error/output to a file and/or to /dev/null ('NUL' for windows):
Runtime.exec("java Test >/dev/null 2>&1");
Runtime.exec("java Test >/dev/null 2>erroroutput");
Redirection is done by the shell processor, not by Runtime.exec() (at least not on Windows).
You need to execute your command by cmd.exe:
String command = "cmd /c java -classpath D:\\dev\\temp\\ Main >NUL 2>test.txt";
proc = Runtime.getRuntime().exec(command);