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?