This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not able to get output file when command is executed using runtime.exec(cmdArray)

 
Satchin Vanir
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have used the below code to execute one .exe file, which will generate the output file based on the input file

public void runExecutableProcess(String program, String in, String out){

try{
String[] cmdArray= new String[3];
cmdArray[0]=program;
cmdArray[1]=in;
cmdArray[2]=out;
System.out.println("Executing "+program+" program");
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(cmdArray);
process.waitFor();

int result = process.exitValue();

if(result!=0){
System.out.println("Exception occured while executing the process "+program);
System.exit(1);
}
BufferedInputStream bis = (BufferedInputStream)process.getInputStream();
System.out.println(" result data "+bis.available());

System.out.println("Successfully executed "+program+" "+in+" "+out+" program");

}catch(IOException ioe){
ioe.printStackTrace();
}catch(InterruptedException ie){
ie.printStackTrace();
}

}
The output file is not generated, when I executed the the same command in windows command prompt output file is generated and getting result data variable as 0.
Please help me in solving this problem.

Many thanks in advance.
Regards
Satchin
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should read When Runtime.exec() won't. In short - read both the output and error contents before calling waitFor(). I prefer to use ProcessBuilder over Runtime.exec though as it allows me to redirect the error stream to the output stream, requiring me to only read one stream instead of two.
 
Satchin Vanir
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:You should read When Runtime.exec() won't. In short - read both the output and error contents before calling waitFor(). I prefer to use ProcessBuilder over Runtime.exec though as it allows me to redirect the error stream to the output stream, requiring me to only read one stream instead of two.


Tanks Rob for the reply.
I tried the options that you specified, reading the output and error streams before waitFor() method andusinng ProcessBuilder, insteadof Runtime.exec.
But I am getting the same problem.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the program you're trying to execute?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic