Thursday, March 6, 2014

How to fix mvn --version or -v Not Recognized Error in Windows but you have Netbeans 7 or higher

I earlier had a difficult time trying to set up Maven for Java. However, Maven does come bundled with NetBeans's newer versions (as of this writing.) Here's how to fix the error that cmd or cygwin does not recognize Maven.

First check whether your netbeans has Maven available in the first place:
From http://stackoverflow.com/questions/12579496/maven-in-netbeans-7-1-1 , user 'baba' :
Netbeans uses an embedded Maven installation.
Open Tools->Options->Java->Maven
where you can see the maven binaries.
If you open the Maven Home dropdown menu, you are going to see a Bundled one (the embedded), and a list of the other maven binaries (for you -> none).
The binary of the bundled maven install is in:
<netbeans_install>/java/maven/bin/mvn  
If you don't have that, your Netbeans hasn't got Maven. Head over to https://github.com/tamingtext/book and see "Building the source."

Next, get into your command prompt of choice  (for me it's currently cygwin; the default for most is perhaps Windows Start > Run > type "CMD"). Run mvn --version. You should get something like the following:

Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 05:51:28-0800)
Maven home: C:\Program Files\NetBeans 7.4\java\maven
Java version: 1.7.0_51, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_51\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 8", version: "6.2", arch: "amd64", family: "windows"
 If not, you'll have some things to fix. ETA, 10 mins.

The things I had to do were make variables, and point them to specific places. Let's start with the"path" variable. You may see it online in other help articles as PATH or something else, %PATH% or so on. For my own purposes, I found it via the following on Windows 8: hit Windows key (on keyboard) and Pause/Break at the same time. This should get you to "System." Then go to "Advanced system settings", which should be on the right hand side in Windows 8. This will take you to the screen "System Properties." From there visit "Environment Variables," which should be near the bottom of the window.

In the new Environment Variables window, you will find two possible variables to make: user variables and System variables. We will only be concerned with System variables for now. So go to the "Path" variable, you will likely need to scroll to find it. There is also PATHEXT, but ignore that. Click to edit "Path." It may  be helpful to copy-past the contents into notepad (NOT ms word.) You'll see that Path contained many 'paths' to different areas on the C drive. They are separated by a ";", except at the end when a ";" is not necessary.

Now that you found your "Path" variable, you're ready to make a new directional toward your maven installation that came bundles with Netbeans. You first need to find you Maven bin. For me, it was at C:\Program Files\NetBeans 7.4\java\maven\bin. Luckily Win8 does have a great search capacity, so try searching for "maven\bin" exactly. (I bring up search via putting the cursor in the upper right hand corner, then looking for the magnifying glass symbol.) You may find a small little file called "mvn" under results, hover over it to confirm that it is maven\bin. Alternatively, you can use
Netbeans uses an embedded Maven installation.
Open Tools->Options->Java->Maven
where you can see the maven binaries. >> again, and just find the location that way.


Once you get that location, copy and paste it into your Path variable mentioned earlier. Remember you can use Notepad or similar to edit it more easily, and separate them with ";", except at the end. Go ahead and save that and hit ok on the System Properties screen as well.

Next, restart you command terminal if it was already open. Just close all instances, then reopen. Perhaps even check the Task Manager to make sure your terminal isn't running. Then, open a new instance. Type mvn --version again. You should get a better message new, saying something similar to
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 05:51:28-0800)
Maven home: C:\Program Files\NetBeans 7.4\java\maven
Java version: 1.7.0_51, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_51\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 8", version: "6.2", arch: "amd64", family: "windows"
from last time. However, there is a decent chance it will tell you that JAVA HOME is not pointing to the right pathway. So if that's the case with your setup, let's continue.

Go back to System Variables and check to see if you have a JAVA_HOME variable. If not, you'll need to make one. Either way, fill it with the location to your Java installation bin. The best option is to point it toward your JDK, not the JRE. The JDK, as far as I know, has both the virtual environment you'll need, but the Java language itself. So it has the best of both things.

To find your JDK, search java.exe, using the same search method as last time. I personally found 79 matching files for the term, but what we want is the java.exe in the JDK folder, NOT the JRE's. In Win8.1 and perhaps others, you can hover over the results to see their full file paths. For me, the JDK path was C:\Program Files\Java\jdk1.7.0_51\.

If you absolutely cannot find your JDK folder, perhaps you don't have it; download it from the oracle site.

After locating the JDK path, put it in the JAVA_HOME system variable.

After this, once more make sure you have no terminals open, then open a new one. And after running mvn --version, your run of Maven should be a success!

No comments:

Post a Comment