Java Runtime.Getruntime(): Getting Output from Executing a Command Line Program

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.

getting output from executing a command multi line program

You are resetting the text everytime, therefore only the last line is kept. Do this:

StringBuilder builder = new StringBuilder()
try {
while ((s = stdInput.readLine()) != null) {
builder.append('\n').append(s);
}

while ((s = stdError.readLine()) != null) {
builder.append('\n').append(s);
}

textArea.setText(builder.toString();
} catch (IOException e2) {
e2.printStackTrace();
}

Runtime.getRuntime().exec() output differ from executing a command line program directly

The commandline shell does some magic for you which Runtime.exec() does NOT for you.

In this case I guess, that the shell interprets (and omits) the ' marks in your commandline.

So please try this version, where the ' have been removed and the commandline has been splitted into parts by hand (another common issue):

String[] args = new String[]{
"/usr/bin/mediainfo",
"--Inform=Video;%Duration%",
"/home/daniel/upload/videos/4/f/6/e/f/4f6ef2e0d67c4.flv"
};
Runtime.getRuntime().exec(args);

Redirect Runtime.getRuntime().exec() output with System.setOut();

The standard output of Runtime.exec is not automatically sent to the standard output of the caller.

Something like this aught to do - get access to the standard output of the forked process, read it and then write it out. Note that the output from the forked process is availble to the parent using the getInputStream() method of the Process instance.

public static void main(String[] args) throws Exception {
System.setOut(new PrintStream(new FileOutputStream("test.txt")));
System.out.println("HelloWorld1");

try {
String line;
Process p = Runtime.getRuntime().exec( "echo HelloWorld2" );

BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()) );
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
catch (Exception e) {
// ...
}
}

How to make Runtime.getRuntime().exec execute command one by one and get output rather than executing all commands

In your case I would not use Threads, you want a sequential execution path.

Java getting output from program executed in command line when it constantly updates a single line in the command prompt

I think I see what's going on. readLine() is looking for a '\n' character. Single-line programs emit a '\r' to return to the beginning of the line and re-write it over and over.

I read around commons-io and javadocs and neither of them have a readLine(char c) which would read until a particular character is encountered, so unfortunately I think you have to code this up for yourself. :\

command does not execute successfully when run through java code using runtime.getruntime command

Your command prompt does much more than just run an executable, which is what Runtime.exec() does. For example, the output-redirection (> yjs.txt 2>&1) is a feature of your command prompt, but not a feature of Java.

If you want to redirect the output to a file, you have two options:

  • put the full command including the output-redirects into a batch-file (or shell-script, depending on your OS) and execute that batch-file from Java.
  • use the Process object returned by exec() and write the output- and error-stream you receive from that object to a file yourself.

Actually, you should do the second case anyway. If the command you start generates lots of output, it may start blocking if that output isn't consumed by your code. Have a look at http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html, it explains in large details the possible pitfalls of Runtime.exec() and also provides solutions for how to deal with it (for example by using the StreamGobbler in listing 4.5 of the article).



Related Topics



Leave a reply



Submit