0

I created a sample spring-boot project on my mac. But when I try to run maven commands on it (such as mvn spring-boot:run), I get the following error:

Exception in thread "main" java.lang.UnsupportedClassVersionError:
org/apache/maven/cli/MavenCli : unsupported major.minor version 51.0

followed of course by the obligatory stack trace. I know from reading answers on many other similar (but not identical) questions, that this has something to do with compiling with a more recent version of Java, and then running with an earlier version of Java. According to this chart:

J2SE 8 = 52
J2SE 7 = 51
J2SE 6.0 = 50
J2SE 5.0 = 49
JDK 1.4 = 48
JDK 1.3 = 47
JDK 1.2 = 46
JDK 1.1 = 45

I must have somehow compiled with Java 7, and then somehow run with Java 6. But I don't understand how this can be so. I'm pretty sure I'm using Java 8. For one thing, when I enter java -version in the terminal, I get:

java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

Also, my pom clearly specifies to use Java 8:

  <project>

    <!-- project info -->

    <name>springboot-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.3.RELEASE</version>
      <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
    </properties>

    <!-- dependencies -->

  </project>

All the answers I've read so far say to compile with Java 6, or to just use Java 8... but I am using Java 8! Also, since the error I'm getting says "51.0" instead of "52.0", doesn't that mean I'm compiling with Java 7? As far as I know, I am only using Java 8!

Most importantly, how do I fix this?

James Dunn
  • 8,064
  • 13
  • 53
  • 87

1 Answers1

2

The problem lay in the fact that I did not have an explicit JAVA_HOME.

When typing which java into the terminal, I would get /usr/bin/java
which is merely a symbolic link pointing to /Library/Java/JavaVirtualMachines/{jdk-version}/Contents/Home/bin/java.

Under the JavaVirtualMachines directory, I had two folders:

  • 1.6.0.jdk
  • jdk1.8.0_25.jdk

So I did infact have a Java 6 installation after all! Even though /usr/bin/java was pointing to the directory for Java 8, and the version in my terminal was Java 8, when I tried to run maven it was using the Java 6 installation!

To fix this I needed to set my JAVA_HOME environment variable and add it to my PATH. Before I did this, when typing echo $JAVA_HOME into the terminal, I got back nothing.

I created a file named .bash_profile in my home directory (since I didn't already have one). I then put the following in it:

  export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
  export MAVEN_HOME=/Applications/apache-maven-3.3.9
  export PATH=$PATH:$MAVEN_HOME/bin:$JAVA_HOME/bin

I restarted my terminal, navigated to my project, entered mvn spring-boot:run, and it worked like a charm!

James Dunn
  • 8,064
  • 13
  • 53
  • 87
  • if you add `export JAVA_HOME=$(/usr/libexec/java_home)` to your `.profile`, then you will always have `JAVA_HOME` set to the current version. `/usr/libexec/java_home -h`shows some more info – P.J.Meisch May 08 '17 at 10:19