114

I installed maven by following this steps:(a tutorial)

JAVA_HOME=/Library/Java/Home
export M2_HOME=/Users/steven/zimmermann/maven
export M2=$M2_HOME/bin
export PATH=$M2:$PATH

echo $JAVA_HOME
echo $M2_HOME
echo $M2
echo $PATH

nano .bash_profile

then I wrote the echo in the .bash_profile sth like this:

JAVA_HOME=/usr/libexec/java_home
M2_HOME=/path/to/your/apache-maven-3.x.x
M2=/path/to/your/apache-maven-3.x.x/bin
PATH=/path/to/maven/bin:/$….bla-bla-bla…

and also I wrote this in the .bashrc

export M2_HOME=/Users/steven/zimmermann/maven
export M2=$M2_HOME/bin
export PATH=$M2:$PATH

now when I want to check the version (mvn -v) I get the following exception: I think there are some versions wrong, but I don't know.

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:637)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    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(ClassRealm.java:401)
    at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:42)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(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(Launcher.java:144)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:266)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)

Additional information:

java -version
java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)
javac -version
javac 1.8.0_40
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Steven
  • 1,262
  • 2
  • 10
  • 11

6 Answers6

275

The problem is because you haven't set JAVA_HOME in Mac properly. In order to do that, you should do set it like this:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home

In my case my JDK installation is jdk1.8.0_40, make sure you type yours.

Then you can use maven commands.

Regards!

GerSua
  • 2,774
  • 1
  • 10
  • 3
  • 15
    I also note that the current version of Maven3 needs Java7, at least I got this error message when `JAVA_HOME` *was* set correctly, but to a Java 1.6 JDK. – Steve Powell Jun 08 '15 at 17:13
  • I needed `export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/"` to my .profile. – aliteralmind Aug 22 '15 at 15:43
  • In my case, the dir `/Library/Java/JavaVirtualMachines` is empty...what should I do? p.s.`java -version java version "1.6.0_65" Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716) Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)` – LittleLittleQ Aug 28 '15 at 07:32
  • 1
    you should add this to your .profile or .bash_profile file depending on the Mac OS X version you have. – Pacu Sep 06 '15 at 18:54
  • also, if added to .profile or .bash_profile, remember to close your terminal / start a new terminal so the contents of this file are run again. – jotadepicas Dec 18 '16 at 01:11
16

Please rather try:

$JAVA_HOME/bin/java -version

Maven uses $JAVA_HOME for classpath resolution of JRE libs. To be sure to use a certain JDK, set it explicitly before compiling, for example:

export JAVA_HOME=/usr/java/jdk1.7.0_51

Isn't there a version < 1.7 and you're using Maven 3.3.1? In this case the reason is a new prerequisite: https://issues.apache.org/jira/browse/MNG-5780

itsmysterybox
  • 2,748
  • 3
  • 21
  • 26
René
  • 354
  • 1
  • 6
  • Hey, I am currently struggling with the same problem.. What do you mean by "Isn't there a version < 1.7"? I am using Java 1.6, is that too old? And the second question: I don't have a "java" folder under "/usr", only under System/Library? Any help would be greatly appreciated – Frederik Witte Mar 26 '15 at 14:03
  • 6
    Beginning from Maven 3.3 there is a minimum requirement JDK 1.7. If you want to compile with JDK 1.6 you got to stuck on Maven 3.2.5 or less. – René Mar 27 '15 at 15:07
  • 1
    I got it to work already :) I had JDK 1.7 installed, but JAVA_HOME was referencing to jdk 1.6 – Frederik Witte Mar 27 '15 at 15:51
7

A dynamic $HOME/.zshrc solution, if you're like me ie. Linux @ work; MBP/A @ home

if [[ $(uname) == "Darwin" ]]; then export OSX=1; fi
if [[ $(uname) ==  "Linux" ]]; then export LINUX=1; fi

if [[ -n $OSX ]]; then
        export JAVA_HOME=$(/usr/libexec/java_home)
else
        export JAVA_HOME=/usr/lib/jvm/default-java
fi
evandrix
  • 6,041
  • 4
  • 27
  • 38
3

I solved it putting a old version of maven (2.x), using brew:

brew uninstall maven
brew tap homebrew/versions 
brew install maven2
javaboygo
  • 243
  • 2
  • 11
2

In Eclipse, you don't need to change JAVA_HOME, you just need to change the run configuration for Maven to something above 1.6 (even if your project is on Java 6, Maven shouldn't be). Right-click the project, choose Maven Build or Run As > Run Configurations and set the correct JDK version.

Noumenon
  • 5,099
  • 4
  • 53
  • 73
0

Do this in your .profile -

export JAVA_HOME=`/usr/libexec/java_home`

(backticks make sure to execute the command and place its value in JAVA_HOME)

Pradeep
  • 148
  • 1
  • 9