Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Runtime.getRuntime(): getting output from executing a command line program

Tags:

java

runtime

I'm using the runtime to run command prompt commands from my Java program. However, I'm not aware of how I can get the output the command returns.

Here is my code:

Runtime rt = Runtime.getRuntime();  String[] commands = {"system.exe", "-send" , argument};  Process proc = rt.exec(commands); 

I tried doing System.out.println(proc); but that did not return anything. The execution of that command should return two numbers separated by a semicolon. How could I get this in a variable to print out?

Here is the code I'm using now:

String[] commands = {"system.exe", "-get t"};  Process proc = rt.exec(commands);  InputStream stdIn = proc.getInputStream(); InputStreamReader isr = new InputStreamReader(stdIn); BufferedReader br = new BufferedReader(isr);  String line = null; System.out.println("<OUTPUT>");  while ((line = br.readLine()) != null)      System.out.println(line);  System.out.println("</OUTPUT>"); int exitVal = proc.waitFor(); System.out.println("Process exitValue: " + exitVal); 

But I'm not getting anything as my output, but when I run that command myself it works fine.

like image 942
user541597 Avatar asked Apr 19 '11 02:04

user541597


People also ask

How to get output from Runtime exec in java?

exec(commands); InputStream stdIn = proc. getInputStream(); InputStreamReader isr = new InputStreamReader(stdIn); BufferedReader br = new BufferedReader(isr); String line = null; System. out. println("<OUTPUT>"); while ((line = br.

How do you interact with Java system at runtime?

In Java, the Runtime class is used to interact with Every Java application that has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime() method.


2 Answers

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.

like image 56
Senthil Avatar answered Sep 23 '22 07:09

Senthil


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; } 
like image 31
735Tesla Avatar answered Sep 19 '22 07:09

735Tesla