Question

OS: Ubuntu 12.10
Eclipse: 4.2
Java: jdk1.5.0_22 and jdk1.7
Apache Maven: 3.0.4
Maven home: /usr/share/maven
m2e: 1.2.020120903-1050

I have created a very simple "Hello, World" Maven Project in Eclipse, in order to test it to migrate our own project (which uses 1.5). My pom.xml is as follows:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>br.com.contmatic</groupId>
  <artifactId>maven-test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>MavenTest</name>
  <description>teste do maven</description>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

I have set J2SE-1.5 (jdk1.5.22) in my project build path. Yet, trying to run Maven Clean or Maven Install from within Eclipse, I get the following error:

Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClassFromSelf(ClassRealm.java:386)
    at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:42)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:244)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:230)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    at org.apache.maven.cli.MavenCli.container(MavenCli.java:375)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:191)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:592)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)

Googling around, I found out that this happens when the jre version used to compile is different than the one used to run. Changing everything to 1.7 (both pom.xml and build path) fixes the errors, which suggest that it either compiles or runs using java 1.7.

But I can't seem to find the error in my configuration.

when running

mvn clean install

in the terminal, maven builds the project without errors.

What could I be doing wrong in Eclipse?

Was it helpful?

Solution

Turns out some of my Maven dependencies weren't compiled with 1.5, which caused the problem. It wasn't happening in the console because I had already set java 1.7 as default.

Fortunately, I was able to build my project using 1.7 and specifying 1.5 to run compile and execute. Here's how:

in Eclipse>Run>Run Configurations>Maven Build>New, I had this configuration:

Main Tab

Base Directory:

${project_loc}

Goals:

clean install

I just chose the JRE tab and chose and Alternate JRE, using my 1.7 SDK installation.

Next, I had to include 1.5 as source and target for the maven-compiler-plugin my pom.xml, as such:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <encoding>ISO-8859-1</encoding>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>

And it works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top