'mvn' is not recognized as an internal or external command

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
6 min

banner

# 'mvn' is not recognized as an internal or external command

The error "'mvn' is not recognized as an internal or external command, operable program or batch file" occurs for multiple reasons:

  1. Not having Apache Maven installed on your machine.
  2. Not having the path to the mvn binary file in your system's PATH environment variable.
  3. Not having Java Development Kit (JDK) installed on your machine.
  4. Not having the path to the java executable in your system's PATH environment variable.

mvn is not recognized as internal or external command

To resolve the error:

  1. Visit the official Apache Maven downloads page and click on the "Binary zip archive" link under "Files", e.g. apache-maven-3.8.6-bin.zip.

download binary zip archive

  1. Open your Downloads directory, right-click on the apache-mavenzip file and click "Extract all".

right click and extract

  1. When you get prompted for the destination folder, click on "Browse" and select the "C:\Program Files" directory and click "Select Folder" > "Extract".

select c program files

Note that this will require you to have administrative permissions. If you don't have elevated permissions pick another folder and make a note of it.

Alternatively, you can extract the zip file in the current directory and copy the apache-maven folder to your preferred location, e.g. C:\Program Files.

apache maven directory location

There is a bin directory in your apache-maven-X.Y.Z directory.

The bin directory contains the mvn executable file which is run when you issue mvn commands using the CLI.

path to bin directory

We have to add the path to the bin directory to your system's PATH environment variable.

# Add the path to the mvn executable to your system's PATH environment variable

To add the path to the mvn executable in your system's PATH environment variable:

  1. Click on the Search bar and type "environment variables".
  2. Click on "Edit the system environment variables".

edit system environment variables

  1. Click on the "Environment Variables" button.

click environment variables

  1. In the "System variables" section, select the "Path" variable and click "Edit".

select path and click edit

  1. You can either click "New" and then "Browse" and navigate to the path of the bin directory or paste it directly.

click new browse

For me, the path is the following.

cmd
C:\Program Files\apache-maven-3.8.6\bin
Note that the path contains your Apache Maven version, so it will likely be different in your case.

We are looking for the bin directory that contains the mvn executable file.

path to bin directory

  1. Once you find the path to your bin directory, add it and click on the "OK" button twice to confirm.

added path to mvn bin directory

  1. Close your Command prompt application and then reopen it.
Note that you must restart your Command prompt shell for the changes to take effect.

You might also have to restart your PC, but that's not always necessary.

Open a new Command Prompt and run the mvn --version command to make sure the mvn executable is accessible.

cmd
mvn --version

mvn command works

If the error persists, make sure that:

  • you don't have any stale CMD, PowerShell or IDE sessions and rerun the mvn --version command.

  • You have a Java Development Kit (JDK) version installed and you have the java executable in your PATH.

cmd
java -version

java get version

If you get an error that java is not recognized, you have to:

  • install Java Development Kit (JDK)
  • have the path to the java executable in your PATH environment variable to be able to use Apache Maven

# Installing and configuring Java Development Kit on Windows

If you already have java installed, scroll down to the "Add the path to the java executable in your system's PATH environment variable" subheading.

To install and configure Java on Windows:

  1. Download the official Java Development Kit installer.

download-java-windows-installer

  1. When you start the installer, you might have to change your app recommendation settings.

change my app recommendation settings

  1. You can set the "Choose where to get apps" value to "Anywhere, but warn me before installing an app that's not from the Microsoft Store".

choose where to get apps

  1. Click on "Install anyway".

click install anyway

  1. Click on the "Next" button.

java-click-next

  1. Leave the default destination location selected and click "Next".

default java destination location

  1. Wait for the installation to complete and click on the "Close" button.

java installed successfully

  1. Restart your Command Prompt and try issuing the java -version command.
cmd
java -version mvn --version

java command works

If you get output for both the java and mvn commands, then everything works as expected.

If the error persists, you have to set the path to the java executable in your system's PATH environment variable.

# Add the path to the java executable in your system's PATH environment variable

To add the path to the java executable in your system's PATH environment variable:

  1. Click on the Search bar and type "environment variables".
  2. Click on "Edit the system environment variables".

edit system environment variables

  1. Click on the "Environment Variables" button.

click environment variables

  1. In the "System variables" section, select the "Path" variable and click "Edit".

select path and click edit

  1. You can either click "New" and then "Browse" and navigate to the path or paste it directly.

click new browse

The path on my machine is the following:

cmd
# ๐Ÿ‘‡๏ธ this is the path on my machine C:\Program Files\Common Files\Oracle\Java\javapath # ๐Ÿ‘‡๏ธ it might also be the following path for you C:\Program Files (x86)\Common Files\Oracle\Java\javapath

path to java exe

We are looking for the directory that contains the java.exe file because this is the file that is run when you issue java commands.

In your case, the java.exe file might also be under a path similar to the following.

cmd
# ๐Ÿ‘‡๏ธ your version will likely be different C:\Program Files (x86)\Java\jre1.8.0_351\bin

If you can't find the PATH, try issuing the where java command.

cmd
where java

find java exe location

The javapath folder doesn't contain the jar.exe file. If you intend to use the jar command to package multiple files into a single archive, you have to add the bin directory as well.

On my machine, the location that contains the java executable files is the following.

cmd
C:\Program Files\Java\jdk-19\bin

Notice that the path contains the jdk version, so your path will likely be different if your jdk version differs.

path to jar exe

  1. Once you find the path to your java executable files, add it and click on the "OK" button twice.

added path to java exe

  1. Close your Command prompt application and then reopen it.
Note that you must restart your Command prompt shell for the changes to take effect.

You might also have to restart your PC, but that's not always necessary.

Open a new Command Prompt and run the java -version command to make sure java is installed.

cli
java -version mvn --version

java and maven work

After the path to the executable (.exe) files has been added to your system's PATH environment variable, the mvn command should work.

If the error persists, try restarting your PC to make sure you don't have any stale CMD, PowerShell or IDE sessions and rerun the mvn --version command.

# Conclusion

To solve the error "'mvn' is not recognized as an internal or external command, operable program or batch file", make sure:

  1. You have Apache Maven installed on your machine.
  2. You have the path to the mvn binary file in your system's PATH environment variable.
  3. You have Java Development Kit (JDK) installed on your machine.
  4. You have the path to the java executable in your system's PATH environment variable.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev