# Windows builds, refreshenv: command not found

**URL:** <https://travis-ci.community/t/windows-builds-refreshenv-command-not-found/5803>\
**Category:** Windows\
**Tags:** build-env\
**Created:** [November 6, 2019, 12:58pm UTC](https://travis-ci.community/t/windows-builds-refreshenv-command-not-found/5803 "2019-11-06T12:58:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/lamba92/32/1171_2.png) [@lamba92](https://travis-ci.community/u/lamba92)\
**Post date:** [November 6, 2019, 12:58pm UTC](https://travis-ci.community/t/windows-builds-refreshenv-command-not-found/5803/1 "2019-11-06T12:58:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tanzislam](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/tanzislam/32/2591_2.png) [@tanzislam](https://travis-ci.community/u/tanzislam)\
**Post date:** [November 10, 2019, 12:11am UTC](https://travis-ci.community/t/windows-builds-refreshenv-command-not-found/5803/2 "2019-11-10T00:11:43Z")

</div>

See my similar answer to another question about using `RefreshEnv.cmd`: [Install location for iisexpress](https://travis-ci.community/t/install-location-for-iisexpress/2902/2)

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/lamba92/32/1171_2.png) [@lamba92](https://travis-ci.community/u/lamba92)\
**Post date:** [November 10, 2019, 10:17am UTC](https://travis-ci.community/t/windows-builds-refreshenv-command-not-found/5803/3 "2019-11-10T10:17:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Marcus10110](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/marcus10110/32/2822_2.png) [@Marcus10110](https://travis-ci.community/u/Marcus10110)\
**Post date:** [March 6, 2020, 3:06am UTC](https://travis-ci.community/t/windows-builds-refreshenv-command-not-found/5803/4 "2020-03-06T03:06:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![akosthekiss](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@akosthekiss](https://travis-ci.community/u/akosthekiss)\
**Post date:** [March 26, 2020, 3:40pm UTC](https://travis-ci.community/t/windows-builds-refreshenv-command-not-found/5803/5 "2020-03-26T15:40:37Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![mdimura](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@mdimura](https://travis-ci.community/u/mdimura)\
**Post date:** [October 4, 2020, 9:56pm UTC](https://travis-ci.community/t/windows-builds-refreshenv-command-not-found/5803/6 "2020-10-04T21:56:41Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![danepowell](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/danepowell/32/4521_2.png) [@danepowell](https://travis-ci.community/u/danepowell)\
**Post date:** [January 28, 2021, 11:27pm UTC](https://travis-ci.community/t/windows-builds-refreshenv-command-not-found/5803/7 "2021-01-28T23:27:40Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jeff5](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/jeff5/32/7051_2.png) [@jeff5](https://travis-ci.community/u/jeff5)\
**Post date:** [August 30, 2021, 10:34am UTC](https://travis-ci.community/t/windows-builds-refreshenv-command-not-found/5803/8 "2021-08-30T10:34:39Z")

</div>

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:

```auto
      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.
