Printing Runtime exec() OutputStream to console
See Benjamin Gruenbaum answer covering ProcessBuilder API available since Java 7.
You need to start a new thread that would read process output happening after Process.waitFor()
. Then process output can be copied the console from that thread.
Process.getOutputStream()
is actually the input, ie, stdin to the process. Process.getInputStream()
and Process.getErrorStream()
return InputStream
objects which are the stdout and stderr of the process. The InputStream
s and OutputStream
are from the caller's perspective, which is the opposite of the process's perspective.
The process's output, stdout, is input to the caller, therefore the caller gets a getInputStream()
and reads the stdout from it:
Process proc = Runtime.getRuntime().exec(cmdArr, env, cwdir);
InputStreamReader isr = new InputStreamReader(proc.getInputStream());
BufferedReader rdr = new BufferedReader(isr);
while((line = rdr.readLine()) != null) {
System.out.println(line);
}
isr = new InputStreamReader(proc.getErrorStream());
rdr = new BufferedReader(isr);
while((line = rdr.readLine()) != null) {
System.out.println(line);
}
rc = proc.waitFor(); // Wait for the process to complete
Process.getOutputStream()
is used for the caller to 'output' data into the stdin of the process. This code could be added after 'proc' is created in the first line of the example above, to send data into stdin of the process:
// Send data to stdin of the process (in development, needs input content)
OutputStream os = proc.getOutputStream();
Bytes content = new Bytes("Hello\nasdf\n adma");
os.write(content.arr, content.off, content.length());
os.close(); // should use try/finally or try/resource
Do the above before reading the output. Remember to close the OutputStream
os (either using try/resource or try/finally). So, the process knows that stdin is complete and it can finish processing the info.
Runtime Exec output
The outputstream of the executed program test
would become the inputstream for your current program run_java_program
. Change your code to this and try:
import java.io.IOException;
public class run_java_program {
public static void main(String[] args) {
try {
Process process = Runtime.getRuntime().exec("java -cp C:\\Users\\96171\\eclipse-workspace\\IR_Project\\src test");
java.util.Scanner s = new java.util.Scanner(process.getInputStream());
System.out.println(s.nextLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
I used Scanner
as I know it returns only one line. Based on your need you can also use apache common utils.
I am not able to get expected output from run time exec
May this is what you are looking for.
Process proc = Runtime.getRuntime().exec("cat /home/uhf/metrics.sh");
String s = null;
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");
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);
}
Java Runtime.getRuntime(): getting output from executing a command line program
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.
Capturing stdout when calling Runtime.exec
You need to capture both the std out and std err in the process. You can then write std out to a file/mail or similar.
See this article for more info, and in particular note the StreamGobbler
mechanism that captures stdout/err in separate threads. This is essential to prevent blocking and is the source of numerous errors if you don't do it properly!
read the output from java exec
Use getErrorStream().
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
EDIT:
You can use ProcessBuilder (and also read the documentation)
ProcessBuilder ps=new ProcessBuilder("java.exe","-version");
//From the DOC: Initially, this property is false, meaning that the
//standard output and error output of a subprocess are sent to two
//separate streams
ps.redirectErrorStream(true);
Process pr = ps.start();
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
pr.waitFor();
System.out.println("ok!");
in.close();
System.exit(0);
Related Topics
Difference in System. Exit(0) , System.Exit(-1), System.Exit(1 ) in Java
Declaring an Unsigned Int in Java
How to Create a Topic in Kafka Through Java
How to Detect Submit Button Clicked in Multiple Submit Buttons Scenario in Single Action Class
Fastest Way to Iterate Over All the Chars in a String
How to Create 2 Separate Log Files with One Log4J Config File
Randomly Select an Item from a List
How to Setsize of Image Using Rescaleop
Naming Threads and Thread-Pools of Executorservice
Varying Behavior for Possible Loss of Precision
How Does Java Resolve a Relative Path in New File()
Jtable Disable Checkbox in Cell
How to Speed Up the Gwt Compiler
Read Whole Text Files from a Compression in Spark
Change Background Color of One Cell in Jtable
Why Are Data Transfer Objects (Dtos) an Anti-Pattern
Selenium Switch Focus to Tab, Which Opened After Clicking Link