今天在配置Maven环境变量的时候出现了以下问题,特此记录下来:
首先配置环境变量:添加以下变量,
M2_HOME --- >>> D:\Maven\apache-maven-3.3.1
path后面添加:;%M2_HOME%\bin
打开cmd命令窗口(本机使用的是WIN7 64位操作系统):
首先查询是否安装了JDK,查看JDK版本信息:
java -version
--->>>>>>
C:\Users\admin>java -version
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
再查询Maven是否配置成功:
C:\Users\admin>echo %M2_HOME%
D:\Maven\apache-maven-3.3.1
C:\Users\admin>MVN -V
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/ma
ven/cli/MavenCli : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
1)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClassFromSelf(Cl
assRealm.java:401)
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(
SelfFirstStrategy.java:42)
at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadCl
ass(ClassRealm.java:271)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm
.java:254)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm
.java:239)
at org.codehaus.plexus.classworlds.launcher.Launcher.getMainClass(Launch
er.java:144)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Laun
cher.java:266)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.jav
a:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(La
uncher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:
356)
从以上信息可以看出,Maven没有配置成功,根据报错信息可以初步判定是JDK版本与Maven版本冲突了,需要换JDK版本!
卸载掉JDK1.8版本的,安装JDK1.7版本,配置好环境变量(配置方法参考之前博客文章),
继续cmd输入上述命令,显示Maven配置成功!
C:\Users\admin>java -version
java version "1.7.0_75"
Java(TM) SE Runtime Environment (build 1.7.0_75-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.75-b04, mixed mode)
C:\Users\admin>echo %M2_HOME%
D:\Maven\apache-maven-3.3.1
C:\Users\admin>mvn -v
Apache Maven 3.3.1 (cab6659f9874fa96462afef40fcf6bc033d58c1c; 2015-03-14T04:10:2
7+08:00)
Maven home: D:\Maven\apache-maven-3.3.1
Java version: 1.7.0_75, vendor: Oracle Corporation
Java home: D:\Javajdkjre\jdk1.7\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
C:\Users\admin>
还有一种情况是配置了 %MAVEN_HOME%,path也添加了,一直报mvn不是内部命令,也就是配置没生效,
解决办法:path中写为绝对路径,不用%MAVEN_HOME%这种方式。可能和操作系统有关系!
切记注意版本问题(没必要追求每个最新的版本,稳定适用最好,不然检查版本问题是件很麻烦耗时的事)!