Variables defined in batfile are displayed as blank when batfile is run using java processbuilder

Viewed 149

I have a batfile to run a tomcat instance on a remote server. The batfile contains a variable for Session ID , and looks as below.

cmdkey.exe /add:Server /user:Server\Username /pass:Password
FOR /F "tokens=3 delims= " %%I IN ('psexec -nobanner \\Server query session ^| findstr "Username"') DO set ID=%%I
echo %ID%
PsExec.exe -accepteula -s -i %ID% \\Server -w "C:\Users\Username\Desktop\CMDTest" cmd /c "start /i "CMDTest" RunCatalina.bat"
cmdkey.exe /delete:Server

The batfile executes as expected when run independently. However, when I run it using the java processbuilder, the Variable ID is passed as blank.

ProcessBuilder Code:

    public static void runBatFileOutput(String batName,String Path) throws Throwable {
        ProcessBuilder pb = new ProcessBuilder("cmd", "/c", batName);
        File dir = new File(Path);
        pb.directory(dir);
        Process p = pb.start();
        InputStream is = p.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        String line = null;
        while ((line = reader.readLine()) != null) {
           System.out.println(line);
        }
    }

1 Answers

I was able to resolve this. We had multiple Tomcats running on the same server, and as a result, the home directory read was incorrect, and as a result, would either not launch tomcat, or would launch a different tomcat. The solution was to have a separate batfile on a different server to run the startup bat, and set the env variables as required for the current tomcat instance. The variables in this case will be specific to the current cmd session, and wo t overwrite system variables.

Related