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
linux - How to tell Java run this Runtime.getRuntime().exec, without waiting what ever command it has to run, simply run it in backend? - Stack Overflow
How to use Runtime.getRuntime().exec() or ProcessBuilder or anyother way to execute a complex/hybrid unix/linux command in java - Stack Overflow
java - Runtime exec on linux - Stack Overflow
Java Runtime.getRuntime().exec() with Linux not working - Stack Overflow
I am new to both Java and Linux, I was trying to use some Runtime.exec() commands that would allow my program to execute commands in Linux such as cd /mnt/ and ls --group-directories-first to list files and directories contained in /mnt/ but I think I am making a problem with the execution.
I tried my code to only include the ls --group-directories-first and it worked like a charm, only problem was, it only listed subdirectories and files in the projects folder. I wanted to make my program go to /mnt/ first so I made my command line to a command array by using exec(String[] cmdarray) format as p = Runtime.getRuntime().exec(new String[]{"cd /mnt/","ls --group-directories-first"}); and when I ran it on linux, it just got executed without any error but also without any feedback/printed lines.
Here is my code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class linCom {
public static void main(String args[]) {
String s;
Process p;
try {
p = Runtime.getRuntime().exec("ls --group-directories-first");
BufferedReader br = new BufferedReader(
new InputStreamReader(p.getInputStream()));
while ((s = br.readLine()) != null)
System.out.println("line: " + s);
p.waitFor();
System.out.println ("exit: " + p.exitValue());
p.destroy();
} catch (Exception e) {}
}
}This worked and printed out:
line: DummyFolder1
line: linCom.class
line: linCom.java
exit: 0
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class linCom {
public static void main(String args[]) {
String s;
Process p;
try {
p = Runtime.getRuntime().exec(new String[]{"cd /mnt/","ls --group-directories-first"});
BufferedReader br = new BufferedReader(
new InputStreamReader(p.getInputStream()));
while ((s = br.readLine()) != null)
System.out.println("line: " + s);
p.waitFor();
System.out.println ("exit: " + p.exitValue());
p.destroy();
} catch (Exception e) {}
}
}This just got executed with no errors but also no prints on the screen.
I expected my program to just go to the /mnt/ directory and print out subdirectories and files on there, but it just got executed with no errors and no printed lines.
I have looked at other entries but could not find any answer to my problem.
According to the docs exec():
Executes the specified string command in a separate process.
So any call to exec() should not block unless you used waitFor() on the returned process of the Runtime .
Here is a small example(Exception handling omitted):
CopyProcess p=Runtime.getRuntime().exec("cmd.exe /c ping 127.0.0.1 -n 10");
System.out.println("Here 1");//this will execute immediately
try {
p.waitFor();
System.out.println("Here 2");//this will only be seen after +- 10 seconds and process has finished
} catch (InterruptedException ex) {
ex.printStackTrace();
}
exec() is not making thread wait until spawned process ends by default. You need to call process.waitFor() explicitly to make current process wait.
I guess that PlayThisSlideShow("PresentationInProjector.jpg"); is being called immediately after exec(). What you see is system making JVM process be running as long as child process is running. I guess there is no way to overcome this easily, to have parent process killed while child process still running.
Why can't you kill presentation projector from Java?
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;
}