How to Install Maven on Mac OS X Manually? Fix UnsupportedClassVersion Error: MavenCli

Last updated
App Shah
Crunchify » macOS Tutorials » How to Install Maven on Mac OS X Manually? Fix UnsupportedClassVersion Error: MavenCli

How to Install Maven on Mac OS X manually

Maven build is the first think we do after working on daily codebase. I usually perform Maven Build hundreds of time in a day. As you may have noticed, on Crunchify almost all of our projects are based on Maven.

Maven is one of the best thing happened to Java Developer community.

With simple maven clean install, maven clean deploy and more command, your word project just builds and deployed with all dependencies. Maven downloads all of the dependencies download to the path specified in settings.xml file.

Do you have a new Macbook Pro and is your Mac running on latest Mac OS X version? If yes, it doesn’t come by default with Maven installed.

Check this out 🙁

bash-3.2$ mvn clean install
bash: mvn: command not found

bash-3.2$ mvn eclipse:eclipse
bash: mvn: command not found

If you have any of below questions then you are at right place:

  • Maven – Installing Apache Maven
  • How to install Maven on Mac OSX
  • How to ensure maven is installed in mac
  • How to install Maven on Mac OS X without Homebrew?

In this tutorial we will go over steps on how to install Maven on Mac OS X. Let’s get started:

Step-1

Download lates Maven version apache-maven-3.6.0-bin.tar.gz from official site.

We are using linux wget command to download it.

Here is a mirror download location: http://mirrors.koehn.com/apache/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.zip

bash-3.2$ mkdir ~/crunchify

bash-3.2$ cd ~/crunchify/

bash-3.2$ pwd
/Users/ashah/crunchify

bash 3.2$ wget http://mirrors.koehn.com/apache/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.zip
--2019-01-17 21:20:50--  http://mirrors.koehn.com/apache/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.zip
Resolving mirrors.koehn.com (mirrors.koehn.com)... 209.240.109.238
Connecting to mirrors.koehn.com (mirrors.koehn.com)|209.240.109.238|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9102386 (8.7M) [application/zip]
Saving to: ‘apache-maven-3.6.0-bin.zip’

apache-maven-3.6.0-bin.zip             100%[==========================================================================>]   8.68M  3.25MB/s    in 2.7s    

2019-01-17 21:20:53 (3.25 MB/s) - ‘apache-maven-3.6.0-bin.zip’ saved [9102386/9102386]

Above command will download .zip file to /Users/ashah/crunchify folder.

Step-2

Go to ~/crunchify/ folder or /Users/ashah/crunchify folder.

Extract apache-maven-3.6.0-bin.zip file using unzip command.

bash-3.2$ cd ~/crunchify

bash-3.2$ unzip apache-maven-3.6.0-bin.zip 
Archive:  apache-maven-3.6.0-bin.zip
   creating: apache-maven-3.6.0/
   creating: apache-maven-3.6.0/lib/
   creating: apache-maven-3.6.0/lib/jansi-native/
   creating: apache-maven-3.6.0/lib/jansi-native/freebsd32/
   creating: apache-maven-3.6.0/lib/jansi-native/freebsd64/
   creating: apache-maven-3.6.0/lib/jansi-native/linux32/
   creating: apache-maven-3.6.0/lib/jansi-native/linux64/
   creating: apache-maven-3.6.0/lib/jansi-native/osx/
   creating: apache-maven-3.6.0/lib/jansi-native/windows32/
   creating: apache-maven-3.6.0/lib/jansi-native/windows64/
   creating: apache-maven-3.6.0/bin/
   creating: apache-maven-3.6.0/conf/

New Maven folder created - Crunchify Tips

Step-3

Next thing is to setup classpath.

Setup maven classpath to your environment variable by updating .bash_profile file.

bash-3.2$ sudo vi ~/.bash_profile

Add below two lines and save the file.

export M2_HOME=/Users/ashah/crunchify/apache-maven-3.6.0
export PATH=$PATH:$M2_HOME/bin

Step-4

How to reload .bash_profile from the command line?

bash-3.2$ source ~/.bash_profile

Or execute below command.

bash-3.2$ . ~/.bash_profile

Step-5

Now try again mvn -version and you are all set.

bash-3.2$ mvn -version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T13:41:47-05:00)
Maven home: /Users/ashah/crunchify/apache-maven-3.6.0
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.2", arch: "x86_64", family: "mac"

bash-3.2$

Maven start working again on Mac OSX terminal window - Crunchify Tips

Facing this error?

Are you getting below error while running command mvn -version?

bash-3.2$ mvn -version
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)

The problem is because you haven’t set JAVA_HOME in Mac properly. In order to fix that add below line to .bash_profile before export M2_HOME line.

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

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

.bash_profile file with JDK Path:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home
export M2_HOME=/Users/ashah/crunchify/apache-maven-3.6.0
export PATH=$PATH:$M2_HOME/bin

And you are all set.

Just try running maven and you won’t see any issue. I hope this tutorial will help you setup Maven path on Mac OSX quickly and easily.

17 thoughts on “How to Install Maven on Mac OS X Manually? Fix UnsupportedClassVersion Error: MavenCli”

  1. I did the same steps inside Document folder, but when I write mvn in my project folder, it show error as “command not found: mvn”. Please help.

    Reply
    • Hi Nargis – could you try with command maven?

      Are you running command in Mac Terminal or Windows command line tool?

      Reply
  2. After completing these steps, I’m still getting the command not found error. Do you know why this is happening?

    Reply
  3. I do get this update:

    Clauss-iMac:Downloads Tim$ mvn
    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)
    Clauss-iMac:Downloads Tim$
    Reply
    • Hi there – seems like an issue with Path and Classpath environment variable. Try exporting JAVA_HOME.

      Also – on you Mac – could you try on Terminal window and provide result?

      echo $JAVA_HOME
      Reply
  4. Hi,

    I got the desired result for mvn –version. but when i give the mvn command i am getting the following error- some no goals specified

    [INFO] Scanning for projects...
    
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] -----------------------------------------------------------------------
    [INFO] Total time: 0.199 s
    [INFO] Finished at: 2015-11-22T16:40:28+05:30
    [INFO] Final Memory: 4M/65M
    [INFO] ------------------------------------------------------------------------
    
    [ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format : or :[:]:. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]
    
    [ERROR] 
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR] 
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoGoalSpecifiedException
    Reply
  5. Hi
    I have the same error although I set the JAVA_HOME and even use the same version as you do. I tried to downgrade to java 7 – still no success. Do you have any idea what could be the problem?

    Reply
  6. I’ve got this error: mvn -version

    Error: Could not find or load main class org.codehaus.plexus.classworlds.launcher.Launcher

    Reply

Leave a Comment