Java – eclipse / ant creates a jar in version 1.9, even if everything seems to be set to 1.8

I have a project built using ant and eclipse, and I want it to be compatible with Java 8

When I try to run jar using java 8, I receive 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 looks like it's being compiled using Oracle's Java 9 (installed on my system), but I can't find the location to specify it in eclipse Everything I see seems to indicate that we are using java 8 (screenshot below)

I think I have to miss something, but I can't figure out where it is What should I do to make this jar compatible with Java 8 / class file version 52.0?

Project > Properties > java build path

Project > Properties > java compiler

Window > Preferences > compiler

Right click ant build file > run as > external tool configuration

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>

Solution

Your $Java_ What is the home environment variable?

I recently had a similar problem. As a result, ant seems to ignore many settings provided by eclipse and only uses $Java_ HOME. I passed in build Find this by placing the following in the XML:

<target name="jdk_version_validation">
    <echo message="Java Version: ${java.version}" />
    <echo message="Java home: ${java.home}" />
    <echo message="JAVA_HOME: ${env.JAVA_HOME}" />
    <echo message="JAVACMD: ${env.JAVACMD}" />
</target>

This leads me to:

jdk_version_validation:
     [echo] Java Version: 1.7.0_80
     [echo] Java home: C:\Program Files\Java\jre7
     [echo] JAVA_HOME: C:\Program Files\Java\jdk1.8.0_131\
     [echo] JAVACMD: C:\Program Files\Java\jdk1.7.0_80\bin

I found that the only reliable way to predict which version ant will build is in build The following actions are enforced in XML:

<property name="ant.build.javac.source" value="1.7"/> 
<property name="ant.build.javac.target" value="1.7"/>

So, my advice to you: try building Place the following in the XML:

<property name="ant.build.javac.source" value="1.8"/> 
<property name="ant.build.javac.target" value="1.8"/>

In order to change the problems described in the OP and eliminate any possibility of "eclipse cheating ant", update:

>Go to Window > Preferences > java > install JRE and try to keep only the target JRE, making sure it is one of the JDK folders (not a sibling folder directly under the Java folder). > Go to project > Properties > java compiler and check enable project specific settings to ensure that the compiler compliance level is set to the target JRE java version

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>