0

As you see I defined JAVA_HOME and M2_HOME for java and maven and added to the path Environmental variables: enter image description here

When I try to execute java or mvn command they are working fine in powershell in windows terminal.

mvn in windows terminal and powershell: enter image description here

But not working if I open a new powershell or cmd enter image description here

Any ideas why they are working inside windows terminal and not standalone powershell? If variable is not wrapped by % % then there is no problem, working fine. For example in first screenshot nvm is working fine both windows terminal and powershell.

3
  • PowerShell does not use % for enviornment variables. Have you rebooted your system since you setup the enviornment variables?
    – Seth
    Dec 8, 2021 at 12:43
  • I noticed that PowerShell can't see the "user environmental variables" (If I run PowerShell as an admin java and mvn commands are working well). I added all user variables to System variables too, now I have no problem.
    – senko
    Dec 8, 2021 at 13:08
  • please, DO NOT post images of code/data/error text. why? lookee ... Why not upload images of code/errors when asking a question? - Meta Stack Overflow — meta.stackoverflow.com/questions/285551/…
    – Lee_Dailey
    Dec 8, 2021 at 20:41

1 Answer 1

0

It looks like you found a solution, but here is the reason it happens:

You set JAVA_HOME and M2_HOME variables in your user environment, but added them to your system environment PATH. In that case, you have a race-condition issue in setting your environment variables. In windows, according to ss64:

When a new process is started, the variables will be loaded in the following order:

  • System Environment Variables
  • Shell Variables (per user)
  • User Environment Variables
  • Shell Variables (other)

So you usually cannot set user environment variables inside system ones and have them get expanded correctly. It seems like the new Windows Terminal may behave in the reverse order or handle PATH expansion differently, but that does not guarantee other processes will behave.

Here's what to look for:

# Powershell (core 7.2) terminal:
PS C:\Users\username> $env:path
C:\Program Files\PowerShell\7;%JAVA_HOME%;
                            # ^Bad, not expanded

# Windows Terminal, running Powershell 7.2:
PS C:\Users\username> $env:path
C:\Program Files\PowerShell\7;C:\Program Files\JavaFolder\jre\bin;
                            # ^Good

VS Code will behave either way, depending on which type of terminal you are using.


It works fine if you set them all in the same environment, so either:

  1. Add JAVA_HOME/M2_HOME to your user PATH and remove from system PATH, or
  2. Set JAVA_HOME/M2_HOME as system variables instead. (looks like this worked for you)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .