Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

How to Set Specific Java Version for Maven in Windows? Example Steps

Maven is a great build tool and most of the Java developer uses Maven to build their projects, but like any other tool, there are some intricacies which you need to know while using it. One of them is that Maven uses the Java version from the JAVA_HOME environment variable and not the PATH environment variable. If you have multiple JDK installed on your machine and getting an Unsupported major.minor version 51.0 or Unsupported major.minor version 52.0 error while building your project but have right Java version in PATH then you may need to check the JAVA_HOME and update it. If you don't have access to do it then you can still change JAVA_HOME using the set command in your local shell and run the Maven's mvn command to build the project

This change will not affect the entire machine and go away as soon as you close the shell. I'll tell you how to do that in this article.

Recently I got a chance to work with QuickFIXJ, an open-source library to parse and process FIX messages. If you don't know the FIX, it stands for Financial Information Exchange protocol and is used heavily for Electronic trading. If you are interested, you can learn more about the FIX in my series of FIX tutorials.

I had a requirement to add a custom FIX tag to parse the incoming messages and for that, I need to extend the FIX dictionary of QuickFIXJ (like a FIX44.xml file) and build the JAR file. In order to do that I cloned the latest release of QuickFIX from GitHub and started building using Maven, only for the build to be failed.

I was getting "org/apache/maven/plugins/resources/ResourcesMojo : Unsupported major.minor version 51.0" error which means Maven is using JDK 1.6 or lower to build the project but the project needed Java 1.7 (See my article on UnsupportedClassVersion to learn more).

I was surprised by this error because I was sure that I am using Java 1.8, which should be enough to build the project. To confirm that I run the following command in the command line window I was using to run Maven:
$ java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b32)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b32, mixed mode)

And it was declaring the correct Java version. Then I realized that when you type java on command prompt it picks the JDK version from the PATH environment variable but Maven uses the JAVA_HOME environment variable to pick the JDK version to build your project and run it's a plugin.

To confirm the JAVA_HOME, I checked the environment variable using the "env" shell command on Windows, and boom, it was pointing to JDK 1.6 which explains the error.

The solution was now easy, I just updated JAVA_HOME and it worked fine. So, just remember that Maven doesn't use PATH to pick the Java version, instead, it uses the JAVA_HOME variable.

If you are getting the Unsupported major.minor version 51.0 or Unsupported major. minor version 52.0 error while building your project or any open source project using Maven, please check the JAVA_HOME environment variable in your machine and change it to JDK 1.7 or JDK 1.8 depending upon the error you are getting. You can check these Java Programming Courses to learn more about class files and how Java works in general.



How to specify Java Version for Maven in Command prompt

The solution was easy in my case but one of my readers reported that he can't change an environment variable in his PC without elevated access or calling Desktop support, which happens mainly in a big organization like Investment banks.

If you are also stuck in a similar situation, don't worry. You can still change the JAVA_HOME environment variable in the local shell you are running the maven command by using the "set" command as shown below:

$ set JAVA_HOME=path-to-jdk-bin-directory

The change will be temporary and only applicable to the shell you are running. It will not affect your machine's JAVA_HOME settings and will go away as soon as you close the shell or open a new shell.

This solution is really nice as you don't need elevated access and can solve the problem immediately. Btw, if you want to learn about Maven itself, I suggest you take a look at these Apache Maven courses,  one of the better courses to start with Maven.

How to specify Java Version for Maven in Windows?






Specific Java version change for Maven

Btw, if you want a more permanent solution without touching the JAVA_HOME environment variable then you can also edit the Maven's start scripts like mvn.bat on Windows and maybe mvn.sh in Linux and specify the JAVA_HOME variable there.

For example, you can change the Maven default version to the specific JDK version by editing mvn.bat file to add the set JAVA_HOME=path-to-jdk-bin-directory line after @REM ==== START VALIDATION ==== in mvn.bat (i.e., %MAVEN_HOME%\bin\mvn.bat) as shown below:

@REM ==== START VALIDATION ====
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_181\bin
if not "%JAVA_HOME%" == "" goto OkJHome

This means whenever you run the mvn command from the command line window in your Windows machine, it will use JDK 1.8.0_181 to run it's plugin and build your project.

Here is the screenshot of the Maven batch file which tells where exactly you need to put the JAVA_HOME variable:




That's all about how to specify the Java version for Maven in Windows. As I have said, Maven doesn't use PATH but uses the JAVA_HOME environment variable. Which means you need to specify the Java version there. If you are running multiple JDK and don't want to change the JAVA_HOME setting or you don't have access to change JAVA_HOME, then just specify the JAVA_HOME environment variable in your local shell and run the mvn command.

Similarly,  If you are getting the Unsupported major.minor version 51.0 or Unsupported major.minor version 52.0 error while building your project or any open source project using Maven, please check the JAVA_HOME environment variable in your machine and change it to JDK 1.7 or JDK 1.8 depending upon the error you are getting.

For example, Unsupported major.minor version 51.0 means JAVA_HOME is pointing to JDK 1.6 or lower and Maven using that to run its compiler plugin to build your project and that needs JDK 7 or higher.  On the other hand Unsupported major.minor version 52.0 means JAVA_HOME is pointing to Java 7 or lower and you need to change it to point to Java 8 or higher to run Maven successfully.


Other Maven articles you may like
  • What is the difference between Maven, ANT, and Jenkins? (answer)
  • How to increase the heap size of Maven? (steps)
  • How to fix the Maven Eclipse Dependency search not working issue? (solution)
  • How to install Maven in Windows 10? (steps)
  • How to build a Java project using ANT? (article)
  • How to create or modify build.xml in ANT? (tutorial)
  • Top 5 Apache Maven Books for Free (books)
  • 10 Points about Maven Java Developer should know (maven)
  • 10 Maven Plugins Every Java Developers should know (plugins)
  • 6 Free Maven and Jenkins Courses for Java developers (courses)

Thanks for reading this article so far. If you find this article useful please share it with your friends and colleagues. If you have any questions or feedback please drop a note.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.