Running executables in Powershell scripts on Windows

I’m trying to set up Windows CI builds for our project, and I can’t reproduce the issue locally:

I have a Powershell scripts which is basically contains this code to find JAVA_HOME:

if ($null -eq $env:JAVA_HOME) {
    $output = [string] (& java -XshowSettings:properties -version 2>&1)
    $env:JAVA_HOME = ($output.Split([Environment]::NewLine) | Select-String "java.home").Line.Split("=")[1].Trim()
}

First of all, I was getting an error when trying to use this script as-is, after running choco install openjdk. I was unable to solve it using refreshenv command, so I ended up adding OpenJDK/openjdk-12.0.2/bin into PATH environment variable. I’m not sure it is the correct way, but still it’s not the main issue.

So the above piece of code leads to the following exception:

Exception Type: System.Management.Automation.RemoteException
Exception Message: Property settings:
Caught an exception:
Exception Type: System.InvalidOperationException
Exception Message: This command cannot be run due to the error: The system cannot find the file specified.

From the line Exception Message: Property settings: I ca guess that Powershell somehow messes up the actual stdout of the java executable (it is the first line in $output).

Unfortunately I can’t reproduce the error locally, because Travis does not run commands in Powershell environment, but rather in Bash environment, and debug builds are not supported on Windows.

Questions:

  1. Am I missing something obvious?
  2. Is there a simple way to reproduce the Travis environment locally?
  3. What is the correct way of updating Path after installing new packages using choco?

Please link to a build showcasing the problem.

Here is one of them (I’ve done a lot of small modifications in order to solve the issue) https://travis-ci.com/exonum/exonum-java-binding/builds/152401412

Imprint