commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Yasser Al Masri <ysralma...@yahoo.com>
Subject [exec] Using Commons Exec for context-sensitive execution
Date Wed, 02 Apr 2014 07:05:08 GMT
What I'm trying to do is simply run a batch file that does some 
preparatory work necessary for the subsequent commands to be executed 
successfully (setting environment variables and stuff). To prove this I 
put together a sample that uses Commons Exec


publicclassTester{publicstaticvoidmain(String[]args)throwsException{Testertester =newTester();MyResultHandlerhandler
=tester.newMyResultHandler();CommandLinecommandLine =CommandLine.parse("bash");PipedOutputStreamps
=newPipedOutputStream();PipedInputStreamis =newPipedInputStream(ps);BufferedWriteros =newBufferedWriter(newOutputStreamWriter(ps));Executorexecutor
=newDefaultExecutor();PumpStreamHandlerioh =newPumpStreamHandler(System.out,System.err,is);executor.setStreamHandler(ioh);ioh.start();executor.execute(commandLine,handler);os.write("export
MY_VAR=test");os.flush();os.write("echo $MY_VAR");os.flush();os.close();}privateclassMyResultHandlerextendsDefaultExecuteResultHandler{@OverridepublicvoidonProcessComplete(finalintexitValue){super.onProcessComplete(exitValue);System.out.println("\nsuccess");}@OverridepublicvoidonProcessFailed(finalExecuteExceptione){super.onProcessFailed(e);e.printStackTrace();}}}But
that prints empty string instead of the word
 "test". Any clues?

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message