0

I have windows machine with two java SDK - 1.4.2_04 and 1.8.0_45. I have simple maven Java project in Eclipse Mars.

After maven project creation vizard my pom xml looked like:
<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>grr</groupId>
    <artifactId>arr</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</project>

When I tryed to run my application it crashed with error:

java.lang.UnsupportedClassVersionError: arr/ff (Unsupported major.minor version 49.0)

        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
    Exception in thread "main" 

Code:

package arr;

    public class ff {

        public static void main(String[] args) {

            System.out.println(System.getProperty("java.version"));

        }

    }

Then I have updated my pom.xml:

<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>grr</groupId>
    <artifactId>arr</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.4.2_04</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <verbose>true</verbose>
                    <fork>true</fork>
                    <debug>false</debug>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

And got project working!

How maven knows where to search for JDK? I suppose maven-compiler-plugin somehow does this. If I change java.version to 1.4.2_16 it still compiles. Why? I don't have 1.4.2_16 on my system.

Manwal
  • 23,450
  • 12
  • 63
  • 93
vico
  • 17,051
  • 45
  • 159
  • 315
  • By changing the `` and `` parameters, you don't change the JDK to be used, but options given to the JDK (probably your 1.8.0_45 here) so that it produces Java 4 compatible code. I guess the compiler plugin is smart enough to understand that `1.4.2_04` and `1.4.2_16` just mean `1.4`. – Tome Aug 26 '15 at 09:23
  • Recommended reading: http://stackoverflow.com/questions/11364761/how-do-i-compile-a-java-with-support-for-older-versions-of-java – Gimby Aug 26 '15 at 09:24
  • Here is a question to put this in a different light: Maven itself is a Java program. What Java runtime will be used to run Maven when you run the 'mvn' shell script? – Gimby Aug 26 '15 at 09:27
  • What about running code then? Output in 1.4 and 1.8 cases generates strings 1.4.2_04 and 1.8.0_40 accordingly. That means in both case different JRE is used. How maven knows what JRE I have? Or it is also possible emulate 1.4 JRE with java 1.8? – vico Aug 26 '15 at 09:38
  • Yes, you can compile to a 1.4 target with 1.8 JDK. It seems that Maven is using 1.8 JDK and you run the app with the 1.4 one. – Fran Montero Aug 26 '15 at 09:56

1 Answers1

0

As seen here, minor version 49 is from 1.5 compiled code. That's because by default, <source> and <target> in maven compiler plugin is 1.5. So you get that error when running your app with 1.4 JDK.

As long as you set <source> and <target> to 1.4, your compiled code is compatible with 1.4 environment.

In your case, it seems that maven is using 1.8 JDK, probably configured as JAVA_HOME but you are running your app with the 1.4JDK.

Fran Montero
  • 1,679
  • 12
  • 24