multithreading - Redirect a system call output to a file with Java -


currently having troubles redirect output of small windows batch console log file. java application needs start runtime.exec() call without waiting finish , still log output. here logger class :

public class batchthreadlogger extends thread {   private process process;   private string logfilepath;   private static final logger logger = logger.getlogger(batchthreadlogger.class);    public batchthreadlogger(process process, string logfilepath) {     this.process = process;     this.logfilepath = logfilepath;   }    public void run() {     try {       // create logging file       file file = new file(logfilepath);       file.createnewfile();        // create writer object       outputstream os = new fileoutputstream(file);       printwriter pw = new printwriter(os);        // catch process output in inputstream       inputstreamreader isr = new inputstreamreader(process.getinputstream());       bufferedreader br = new bufferedreader(isr);        // wait process complete       int processstatus = process.waitfor();        // redirect output log file       string line = null;       while ((line = br.readline()) != null) {         pw.println(line);       }        // add small message return code log       pw.println("********************************************");       pw.println("********************************************");       pw.println("batch call completed return status " + processstatus);        pw.flush();       os.close();     }     catch (ioexception e) {       logger.error("ioexception raised during batch logging on file " + logfilepath, e);     }     catch (interruptedexception e) {       logger.error("interruptedexception raised during batch process execution", e);     }   } } 

my call quite simple :

process process = runtime.getruntime().exec(command); batchthreadlogger logger = new batchthreadlogger(process, logfilepath); logger.start(); 

my command calling test.bat 2 parameters. test batch :

echo "batch called parameter %1 , %2" exit 

my log file contains :

******************************************** ******************************************** batch call completed return status 0 

i tried place waitfor()call before , after code redirecting output log file, without success. see black screen of command being launched, nothing in logs...

any appreciated, i'm missing something, cannot understand what...

you're not reading standard error of process create.

i suspect error message being written standard error, , because you're reading standard output, you're not picking error.

i recommend replacing use of runtime.getruntime().exec(...) processbuilder, using following:

processbuilder pb = new processbuilder("cmd.exe", "/c", "test.bat", "one", "two"); pb.redirecterrorstream(true); process process = pb.start(); 

the line pb.redirecterrorstream(true); redirects process' standard error standard output, don't have read 2 streams (standard output , standard error) in 2 separate threads.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -