How To Redirect Output From Java Callout Runtime.exec() To A File On Unix Without Using Process.getInputStream() (Doc ID 430403.1)

Last updated on JULY 05, 2017

Applies to:

Oracle Server - Enterprise Edition - Version: 10.2.0.2 and later   [Release: 10.2 and later ]
Information in this document applies to any platform.
***Checked for relevance on 23-Feb-2010***

Goal

Having some Java code to run an external process like

public class OSCommand{
public static String Run(String Command){
try{
Runtime.getRuntime().exec(Command);
return("0");
}
catch (Exception e){
System.out.println("Error running command: " + Command +
"\n" + e.getMessage());
return(e.getMessage());
}
}
}

we want to redirect the output of this process to a file. However, we do not want to have to deal with Java's Process.getInputStream() and Process.getErrorStream() because we do not want to do so much Java coding.

And calling that method with Unix-like input redirection like

OSCommand.Run ("ls -alR >/tmp/allFiles.lst")

cannot work because the Run method only calls the ls command and nothing that would take care of the output redirection.

Solution

Sign In with your My Oracle Support account

Don't have a My Oracle Support account? Click to get started

My Oracle Support provides customers with access to over a
Million Knowledge Articles and hundreds of Community platforms