Search code examples
javamavencmdmingw32

Why there are two java path in my PC when i use the "mvn -v"


When I use mvn spring-boot:run to run a Spring Boot project, I find if use the command in Cmd, it works. but if I use it in Git Bash, it can't work.

So I use java -version and mvn -v to view the Java and maven version.

java -version give me the same result, but mvn -v result is different, as the picture shows: terminal output 1

And when I see my java path, I'm sure it's C:\Program Files\Java\jdk1.8.0_151

terminal output 2

So I want to know, why there are two Java paths in my PC when I use mvn -v?


Solution

  • Both the java executable and Maven by default use the JDK that is specified in the $JAVA_HOME environment variable.

    You are executing java and Maven (mvn) in different environments (cmd.exe in one case and Git Bash in the other case), so the most likely explanation is that $JAVA_HOME is set to different JDKs in the respective environments.

    You can check the content of $JAVA_HOME with echo $JAVA_HOME.