Windows builds, refreshenv: command not found

I am in need to refresh PATH or JAVA_HOME after installing a JDK using choco install openjdk11 -y.

If I set evn: JAVA_HOME="...." manually it works just fine but I have to hardcode the path to the specific version of the JDK, which is not ideal.

refreshenv command seems to be the ideal solution and everywhere it is said to just call it and you are done, but when calling it inside a Travis Windows build I get: refreshenv: command not found.

I then decided to copy-paste the refreshenv code into a file in the project and call it from there. It gets actually called correctly, but it does not refresh the environment.

Any advice?

1 Like

See my similar answer to another question about using RefreshEnv.cmd: Install location for iisexpress

1 Like

In the end I solved using Gravis, but thank you for the answer :slight_smile:

Is there a proper solution for this? I have the same problem with choco install awscli, however I can’t call awscli using the mentioned cmd.exe solution (cmd.exe //c "RefreshEnv.cmd & ...) because it’s called directly from some node.js code somewhere.

Proper? I don’t know. But I have come up with a single line approach that works for me:

eval $(powershell -NonInteractive -Command 'write("export PATH=`"" + ([Environment]::GetEnvironmentVariable("PATH","Machine") + ";" + [Environment]::GetEnvironmentVariable("PATH","User")).replace("\","/").replace("C:","/c").replace(";",":") + ":`$PATH`"")')

This is not a complete replacement for refreshenv, so I only call it “refreshpath”.

With YAML anchors, it also works nicely in my travis configuartion, e.g.:

_anchors:
  - &refreshpath eval $(powershell -NonInteractive -Command 'write("export PATH=`"" + ([Environment]::GetEnvironmentVariable("PATH","Machine") + ";" + [Environment]::GetEnvironmentVariable("PATH","User")).replace("\","/").replace("C:","/c").replace(";",":") + ":`$PATH`"")')

os: windows
language: shell
before_install:
  - choco install openjdk python3
  - *refreshpath
  - python --version
  - pip --version
  - java -version
1 Like

Here is a relatively simple snippet I use to update the PATH in windows builds:

export PATH=$(cmd.exe //c "refreshenv > nul & C:\Progra~1\Git\bin\bash -c 'echo \$PATH' ")
1 Like

Be careful, I tried the script using PowerShell provided by akosthekiss and while it fixed paths provided by Choco, it also broke things in some very subtle and confusing ways. For instance, Travis CI caching started failing with a cryptic error obviously unrelated to caching:

store build cache
Windows Subsystem for Linux has no installed distributions.
Distributions can be installed by visiting the Microsoft Store:

The smaller snippet provided by mdimura worked great and had no apparent side effects. Thanks!

1 Like

I’ve been wrestling with the Windows CI environment and a project that needs Java on the PATH. So my case is almost exactly that of the OP @lamba92. I install Ant too, but Chocolatey puts it in a place already on the path so that’s ok: not so Java, and the path contains patch-level version, so quoting it would be fragile.

After spending a weekend on this, I thought I should share my solution to spare others. I started with the solution by @akosthekiss (thanks). My idea is to get out just the information I need from the sub-shell, rather than the whole path. Here’s the whole before_install section, almost exactly:

      before_install:
        # Install the required JDK and ant
        - choco install openjdk11 ant

        # Java is at /c/Program Files/OpenJDK/openjdk-<version-patch>/bin which is liable to change.
        # Chocolatey has updated the PATH for Java, but only in powershell [Environment].
        # Dig out the Java path from [Environment] and translate it to Unix.
        - java_exe=$(powershell -NonInteractive -Command
            '$env:PATH=[Environment]::GetEnvironmentVariable("PATH","Machine");
                write((Get-Command java).Path)')
        - java_exe=$(cygpath "$java_exe")
        - echo $java_exe
        - export JAVA_HOME="${java_exe%/bin/*}"
        - echo $JAVA_HOME
        - export PATH="$JAVA_HOME/bin:$PATH"

RefreshEnv sounds like the right thing to use instead of $env:PATH=[ ... but I couldn’t get that to work.

Why not the accepted solution? I didn’t like the solution @lamba92 accepted (from @tanzislam) because that runs the target software in the sub-shell (cmd) right away, and I have some more things to do in the top-level (bash) shell. The one @lamba92 actually used (Gravis) seemed complicated and has gone out of support.

Why not @akosthekiss’s solution directly? @danepowell reports some confusing side effects. I also noticed some as I experimented with it. I guess that the PATH excavated by that route is not quite the one Git Bash needs. The version I offer keeps the PATH shenanigans within the sub-shell, extracting just a Java home directory.