Eclipse/Ant Making a jar in version 1.9, even though everything seems to be set for 1.8

Viewed 1256

I have a project I build using ant and eclipse which I'd like to have be compatible with Java 8.

When I try running the jar using Java 8, I get the following error:

$ /usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar hypnos.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError:
org/joshuad/musicplayer/MusicPlayerUI has been compiled by a more recent version of
the Java Runtime (class file version 53.0), this version of the Java Runtime only 
recognizes class file versions up to 52.0

So it appears that it's compiling using Oracle's Java 9 (which is installed on my system) but I can't find the place in eclipse where this is designated. Everything I see seems to suggest that we're using java 8 (screenshots below).

I feel like I must be missing something, but I can't figure out where. What do I have to do to get this jar to be compatible with Java 8 / class file version 52.0?

Project > Properties > Java Build Pathenter image description here

Project > Properties > Java Compiler enter image description here

Window > Preferences > Compiler enter image description here

Right Click Ant Build File > Run As > External Tools Configuration enter image description here

Ant Build File

<project name="Hypnos Music Player" default="compile" basedir=".">
    <property name="src" location="src"/>
    <property name="build" location="build"/>
    <property name="jarFile" location="hypnos.jar"/>

    <path id="class.path">
        <fileset dir="lib">
            <include name="**/*.jar" />
        </fileset>
        <pathelement location="${jarFile}" />
    </path>

    <target name="init">
        <tstamp/>
        <mkdir dir="${build}"/>
    </target>

    <target name="compile" depends="init" description="compile the source">
        <javac fork="yes" includeantruntime="false" srcdir="${src}" destdir="${build}">
            <classpath refid="class.path" />
        </javac>
    </target>

    <target name="jar" depends="compile" description="Create a jar.">
        <jar destfile="${jarFile}" basedir="${build}">
            <manifest>
                <attribute name="Main-Class" value="org.joshuad.musicplayer.MusicPlayerUI" />
                <attribute name="Class-Path" value="lib/commons-cli-1.4.jar lib/jaad-0.8.4-nospi.jar
                                                    lib/jaudiotagger-2.2.6-SNAPSHOT.jar lib/jflac-1.2.jar
                                                    lib/jl1.0.1.jar lib/jogg-0.0.7.jar lib/jorbis-0.0.15.jar
                                                    lib/vorbisspi1.0.3.jar" />
            </manifest>
        </jar>
    </target>
</project>
2 Answers

In addition to what datv said above, I'll add from experience:

Ant's javac, java, and junit tasks have a jvm attribute that determines how to run the JDK/JRE, if fork="true". Unfortunately, it only sets the raw launcher, and doesn't change all the other settings. When I set the jvm attribute to the full path of a Java 8 JRE, turning on ant's -d switch shows that it is being used as the executable, but followed by

'-Djava.specification.version=9'
'-Dsun.boot.library.path=C:\Java\x64\jdk-9.0.1\bin'
'-Djava.home=C:\Java\x64\jdk-9.0.1'
'-Djava.runtime.version=9.0.1+11'
'-Djava.version=9.0.1'
'-Djava.library.path=...;C:\Java\x64\jdk-9.0.1\bin;.....
'-Djava.vm.version=9.0.1+11'
'-Djava.class.version=53.0'

all of which take precedence.

It turns out that Ant cares more about the JAVACMD environment variable when it starts up. In the situation above, our build framework had set JAVACMD to C:\Java\x64\jdk-9.0.1\bin\java, and the individual jvm= and executable= attributes on specific tasks were... not "ignored" really, merely pointless and misleading.

Related