wood burning stoves 2.0*
The moose likes Java in General and the fly likes Not able to get output file when command is executed using runtime.exec(cmdArray) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Not able to get output file when command is executed using runtime.exec(cmdArray)" Watch "Not able to get output file when command is executed using runtime.exec(cmdArray)" New topic
Author

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

Satchin Vanir
Greenhorn

Joined: Jul 17, 2012
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Satchin Vanir
Greenhorn

Joined: Jul 17, 2012
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

What's the program you're trying to execute?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to get output file when command is executed using runtime.exec(cmdArray)
 
Similar Threads
Problem in unzipping a folder using java.util.zip API
runtime.exec problems
Runtime.exec problem
Help needed for running for this runtime code
MDI file close.