File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • 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
  • 0
  • 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: 20393
46
Chrome Eclipse IDE Java Windows
  • 0
  • 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
  • 0
  • 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: 20393
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the program you're trying to execute?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic