Compiling Java 1.6 projects using Maven on Mac OS X

by Dhruba Bandopadhyay on Mon, 22 Dec 2008 17:34:41 +0000

The following maven pom file configuration works on Windows and Linux but not on Mac OS X.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
</plugin>

The error that occurs is as follows.

Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure
Failure executing javac, but could not parse the error:
javac: invalid target release: 1.6

The reason is that the following environment variable isn’t set by default.

export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home"

You can declare this either as a one time export in your current shell above, in /etc/profile as I normally do or in any other startup file of your choice. Afterwards be sure to refresh your shell before you try again. This can be done either by closing your current terminal window and opening a new one or by sourcing whichever file you’ve put the above information into.

source /etc/profile

In order to make sure that it has taken effect one can output it in the shell as follows.

echo $JAVA_HOME

If it shows nothing then the shell has not picked up your change.

If your tomcat had already been started prior to setting the environment variable then don’t forget to restart it in the new shell otherwise you will get the following error on startup when deploying your maven application.

Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class service.ServiceImpl)
	at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1851)
	at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:890)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1354)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
	at org.springframework.util.ClassUtils.forName(ClassUtils.java:211)
	at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:385)
	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1138)
	... 40 more

Check that your compiler and loader are using the same version of Java.

{ 7 comments… read them below or add one }

Dave December 24, 2008 at 20:58

I’m pulling my hair out with this….I’ve double checked that my JAVA_HOME and PATH env vars are set up correctly, but I’m STILL getting the “Target 1.6 not supported” error. I want to use Java6 because I want to use the JAXB stuff without having to set up dependencies on these libraries in Maven under Java5.

Any other tips that you can recommend for me? (email response preferred :)

Dave

Reply

Dave December 24, 2008 at 21:34

Ahhh…found it. I’m using Intellij IDEA, and the “Maven Runner” preference was set to use an internal Javac …. switching that to use Java6 fixed my problem ;)

Reply

Dhruba Bandopadhyay December 24, 2008 at 21:46

Dave I see. Generally I eliminate the IDE as a problem cause by switching to command line entirely and if it works then I know the IDE is at fault. Glad you found your solution. Merry Christmas! Isn’t it a bit of an odd time to be doing such things? I’m guilty of the same though :-)

Reply

Raul May 31, 2009 at 02:06

It works!

Thanks for the tip

Reply

Tony Almazzo July 16, 2009 at 17:50

Thank you very much! I was changing the JVM in the java tool in the Utility folder but nothing happened.

Reply

Ross Huggett November 27, 2009 at 15:41

Thanks. That got me out of a very dark place.

Reply

Villemos May 21, 2010 at 22:29

Had the same problem on Windows. Tried the tips here, but didnt solved the problem. Only by changing the Java version in Eclipse to point to the Java 1.6 JDK instead of 1.6 JRE installation solved the problem (Window->Preferences->Java->Installed JREs, add JDK folder and tick the box).

Reply

Leave a Comment

{ 4 trackbacks }

Previous post:

Next post: