PowerShell script does not return

310 Views Asked by At

I write a PDF generator that is implemented in Java and uses PowerShell scripts to compile TEX files with latexmk. An example for a generated command is powershell.exe -NoProfile -NoLogo "&'C:\path\to\scripts\compileTex.ps1' 'C:\path\to\workingDir' 'filename.tex'". If I this command manually in the cmd the command compiles the TEX file and returns properly. When I call this script via Java it does not return. I read many other questions that suggest adding < NUL at the end of the command. I do this by explicitly closing the input stream of the process. Resulting from that all PowerShell scripts return properly except for the one with the latexmk command.

I use the following PowerShell script for compilation:

param(
    [Parameter(Mandatory = $true)][string] $workingDir,
    [Parameter(Mandatory = $true)][string] $texFileName
)

if (Test-Path -LiteralPath "$workingDir") {
    cd "$workingDir"
    if (Test-Path -LiteralPath "$texFileName") {
        $texFileBaseName = $texFileName.Trim(".tex")
        latexmk -nobibtex -norc -pdf -jobname="$texFileBaseName" "$texFileName" # FIXME This call leads to a timeout
    } else {
        Write-Error "Could not find \"$texFileName\""
    }
} else {
    Write-Error "The working directory \"$workingDir\" does not exist"
}

I use the following Java snippet for calling this script:

        StringJoiner innerCommand = new StringJoiner(" ", "\"&", "\"")
                .add(String.format("'%s'", script.normalize().toString()));
        for (String param : params) {
            if (param.startsWith("-")) { // Its an option or switch
                innerCommand.add(param);
            } else {
                innerCommand.add(String.format("'%s'", param));
            }
        }
        StringJoiner command = new StringJoiner(" ")
                .add("powershell.exe")
                .add("-NoProfile")
                .add("-NoLogo")
                .add(innerCommand.toString());

        try {
            Process process = Runtime.getRuntime()
                    .exec(command);
            process.getOutputStream().close(); // Make sure CMD does not wait for further commands
            boolean exited = process.waitFor(COMMAND_TIMEOUT_VALUE, COMMAND_TIMEOUT_UNIT);
            String stdOutResult = new String(process.getInputStream().readAllBytes());
            T commandResult;
            if (exited) {
                if (process.exitValue() == 0) {
                    commandResult = onSuccess.apply(stdOutResult);
                } else {
                    String stdErrResult = new String(process.getErrorStream().readAllBytes());
                    commandResult = onFailure.apply(stdOutResult, stdErrResult);
                }
            } else {
                commandResult = onTimeout.get();
            }
            return commandResult;
        } catch (IOException | InterruptedException ex) {
            throw new SystemCommandFailedException(String.format("Processing %d errored", commandID), ex);
        }
    }

EDIT: Appending a [Console]::Out.Flush() does not do the trick.

0

There are 0 best solutions below