zeroturnaround / zt-exec

ZeroTurnaround Process Executor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Executable not found in PATH directories on Windows

fhossfel opened this issue · comments

Just some background: We are trying to run testcontainers on Windows with an internal repository hosted by Google. This requires to run the"docker-credential-gcloud" (from Google Cloud SDK) to obtain credentials by using zt-exec. Eventhough the bin folder of the SDK is listed in the Path environment variables the ProcessExecutor is not able to run it and fails with the error message Cannot run program "docker-credential-gcloud": CreateProcess error=2, Das System kann die angegebene Datei nicht finden (meaning "The system could not find the specified file."). However, if we start the same command through a cmd-shell it works.

Is this a known issue? I could not find any documentation how %Path% is interpreted by zt-exec (or the underlyin
g JDK functionaliy).

Please find below a test case to document the issue. It is the exact same call used by testcontainers:

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.zeroturnaround.exec.ProcessExecutor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class DockerCredentialsTest {
    private String credentialHelperName ="docker-credential-gcloud"; 
    private String hostName = "europe-west1-docker.pkg.dev";

  @Test
    public void testProcessExecutor() throws IOException, InterruptedException, TimeoutException {

        // Uncommenting /*"cmd", "/c",*/  makes the call successful.
        String result = new ProcessExecutor()
                .command(/*"cmd", "/c",*/ credentialHelperName, "get")
                .redirectInput(new ByteArrayInputStream(hostName.getBytes()))
                .readOutput(true)
                .exitValueNormal()
                .timeout(30, TimeUnit.SECONDS)
                .execute()
                .outputUTF8()
                .trim();
        System.out.println(result);
        Assertions.assertNotNull(result);
    }

PATH is implemented by the specific shell (bash, fish, zsh, etc). zt-exec does not use any shell for execution, so you have to specify the full path to the executable. In case you want to make use of some shell feature (like PATH), you could invoke the shell which would in turn invoke the command.

Thanks for the quick reply. This is what I expected. I think this should be mentioned somewhere in the documentation because it is a severe portability issue between *nix and Windows.

However, I am not entirely sure whether this is an issue of zt-exec or of the underlying ProcessBuilder. I am not even sure if there is a defined behavior across different JDK implementations. I have found nothing so far.

I will file a PR against testcontainers to resolve the issue. So from my point of view the issue can be closed.