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.
java - Safe usage of Runtime.getRuntime.exec(String[]) - Information Security Stack Exchange
Runtime.getRuntime().exec leaving processes running?
process - How to get java getRuntime().exec() to run a command-line program with arguments? - Stack Overflow
Runtime.getRuntime().exec()
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;
}
Hello all, I've been looking for an answer on this but can't find anything specific. I have an application that is running quite a few runtime.getRuntime().exec processes independently. This is done to run things from the command prompt like ping, WMI commands, etc. and also runs some powershell commands and runs some scripts. I have noticed that after running a few, I can go into task manager and see quite a few powershell and cmd applications open. Is this not closing? I'm pretty new to the idea of garbage collection, but I thought Java took care of that by itself? Do I need to run something to close these once the process has finished running? Thank you for your help!
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()));
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
I tried using the following function:
Runtime.getRuntime().exec("DIR")but it doesn't get executed and my IDE always shows the following warning:
The method exec(String) from the type Runtime is deprecated since version 18Java(67110270)