Pregunta

I need a solution that allows to capture the batch file exit code and the resultant environment - by this I mean that I need to retrieve the system environment + variables set within the batch.

For better understanding here is what I came up with. Unfortunately the printEnvironment() method does not print out the variable MyVar set previously in batch but only the system variables. Is there a way to capture "MyVar" without changeing the 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
¿Fue útil?

Solución 2

I think I have found kind of a solution. Not perfect but better than nothing. To get the full environment left after batch execution I used different approach for fireing it up.

Simply I have started the batch in cmd which allowed me to pass additional commands as arguments. Next I had to read the output from the output stream. To my supprise I was also able to get the proper exit code from the batch - note though I had to use the clause:

exit /B <exitCode> 

This are mods to the code above:

pb = new ProcessBuilder("cmd.exe", "/c", command, "&&set", "&&exit");
process = pb.start();       
pb.redirectErrorStream(true);

process.waitFor();
exitCode = process.exitValue();

stdout = process.getInputStream ();
stdOutReader = new BufferedReader (new InputStreamReader(stdout));

...

String outLine;

while ((outLine = stdOutReader.readLine ()) != null) {
    if(outLine.trim().length() > 0){
        System.out.println ("OUT STREAM: " + outLine);
    }    
}

Any comments and alternate solutions are most welcome.

Otros consejos

before your code exits try doing this :-

Properties props = System.getProperties();
props.list(System.out);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top