Execute batch files from JAVA and capture batch environment -


i need solution allows capture batch file exit code , resultant environment - mean need retrieve system environment + variables set within batch.

for better understanding here came with. unfortunately printenvironment() method not print out variable myvar set in batch system variables. there way capture "myvar" without changeing batch file itself?

import java.io.ioexception; import java.util.arraylist; import java.util.collections; import java.util.list; import java.util.map;  class main{     public static void main(string[] args){         string command = "c:/temp/vartest.bat";          mytask mt = new mytask(command);         mt.run();     } }  class mytask implements runnable{            private processbuilder pb;     private process process;     private int exitcode;     private map<string, string> env;      private string command;      public mytask(string command){         this.command = command;     }      public void run(){         try {             pb = new processbuilder(command);             process = pb.start();              process.waitfor();             exitcode = process.exitvalue();               } catch (ioexception e) {             e.printstacktrace();                 } catch (interruptedexception e) {             e.printstacktrace();         }finally{             system.out.println("execution finished! exit code: " + exitcode);              printenvironment();             process.destroy();          }                }      private void printenvironment(){         env = pb.environment();          list<string> envkeys = new arraylist<string>(env.keyset());         collections.sort(envkeys);          for(string key : envkeys){             system.out.println(key+" ==> "+env.get(key));         }     } } 

the batch file code:

set myvar=val 

before code exits try doing :-

properties props = system.getproperties(); props.list(system.out); 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -