Levi
Levi

Reputation: 859

VS Code Showing 'mvn' is not recognized as an internal or external command

I'm trying to create a maven project with VS Code but when I run the command it says :

'mvn' is not recognized as an internal or external command,operable program or batch file.

but mvn -version is running on command prompt

Environment varaible for User :

System Variables

path is set to C:\apache-maven-3.6.1\bin, C:\Program Files\Java\jdk1.8.0_212\bin for maven and java respectively.

The command I'm running to create the project is: mvn archetype:generate -DgroupId=com.cs.test-project -DartifactId=test-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false The Reference to Command

And the VS Code Reference to Command

The same command is running in Command prompt and it created the project successfully on Desktop.

Upvotes: 12

Views: 30510

Answers (5)

Talijanac
Talijanac

Reputation: 1157

It's kinda counterintuitive as path has to be pointed to the actual mvn command, instead to a M2_PATH folder. Also once properly pointed, a new error will be shown if your environment is missing proper jdk within JAVA_HOME. The error is trown by maven. Maven plugin is kinda stupid as it will ignore your default JDK configured within settings.json (java.home or/and java.configuration.runtimes properties) and will happly NOT set java for maven. You have to configure it specificly for maven plugin as this:

"maven.executable.path": "c:\\apache-maven-3.8.1\\bin\\mvn",
"maven.terminal.customEnv": [{
        "environmentVariable": "JAVA_HOME",                
        "value": "c:\\openjdk-1.8.0_232-redhat",
}]

Of course, both paths should be pointed to proper folders/files in your environment.

Upvotes: 5

ChrisoLosoph
ChrisoLosoph

Reputation: 607

I am using Ubuntu 20.0.

In my case, I added the path to the Maven binary directory manually to the $PATH variable. I accidentally only added it to the environment variables in the shell but not to the user session.

Also make sure you are using the official binary that you can download from Maven's website and not the Ubuntu APT package because that one is too old and did not work for me.

Let's assume, the Maven executable has been stored in ~/apache-maven/bin. Then you can edit ~/.profile by adding export PATH+=":$HOME/apache-maven/bin".

Restart the computer and then, when you login to your profile, mvn should be visible by programs as well as VSCode.

Upvotes: 0

armando rael
armando rael

Reputation: 101

On Tuesday Oct. 25, 2022 while trying to solve the same problem, I went through all of these previous solutions for Windows and finally had to resort to the official installation docs:

Installing Apache Maven

Unzipped the download and located the bin directory and manually added that directory location to my: System Properties>Advanced>Environment Variables>'Path'

For good measure, restart the computer.

Upvotes: 2

Norbert B.
Norbert B.

Reputation: 1

The way I fixed was by changing the exec path in Vs Code settings from CMD to Powershell.

Open

settings -> features -> terminal

Change the windows exec path from cmd to where you have PowerShell.

For example: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Windows exec setting

Upvotes: 0

KP Singh
KP Singh

Reputation: 384

I fixed this problem by simply restarting VSCode as I had set my PATH variable while my VSCode was running so the changes were not reflected to the VSCode integrated terminal until I restarted it.

Upvotes: 7

Related Questions