Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试引用Jar文件时,Eclipse出现不支持的major.minor版本51.0错误_Java_Eclipse_Ant - Fatal编程技术网

Java 尝试引用Jar文件时,Eclipse出现不支持的major.minor版本51.0错误

Java 尝试引用Jar文件时,Eclipse出现不支持的major.minor版本51.0错误,java,eclipse,ant,Java,Eclipse,Ant,我正在编译一个针对java版本6的jar文件,但是我遇到了一个与不支持的major.minor版本51.0相关的错误。在阅读了几篇文章后,例如我无法解决我的问题,并采取了极端的步骤试图隔离问题,如下所述 我创建了一个java沙盒项目,其目的是测试Xerces For Android jar,我正在从源代码制作该jar,目标是版本6。如果我把源代码直接放到项目中,我就能够成功地编译和运行我的测试程序。但是,如果我创建了一个jar文件,然后将其添加到构建路径中,我会得到不支持的major.minor

我正在编译一个针对java版本6的jar文件,但是我遇到了一个与
不支持的major.minor版本51.0
相关的错误。在阅读了几篇文章后,例如我无法解决我的问题,并采取了极端的步骤试图隔离问题,如下所述

我创建了一个java沙盒项目,其目的是测试Xerces For Android jar,我正在从源代码制作该jar,目标是版本6。如果我把源代码直接放到项目中,我就能够成功地编译和运行我的测试程序。但是,如果我创建了一个jar文件,然后将其添加到构建路径中,我会得到
不支持的major.minor版本51.0

阅读了几篇文章后,不支持的主次版本51.0似乎是一个基于不同java版本编译的问题。51.0版看起来是Java7。为了简化测试,我从我的机器上卸载了所有Java 7安装,确保我的eclipse项目指向相同的Java 6 JRE,并且我的Java_HOME设置为Java 6u45。我还重新启动了eclipse,以确保我的更改到位

我正在使用
Ant
创建jar文件。代码非常简单,我甚至将javac目标指定为1.6

<project name="XercesForAndroid" default="dist" basedir=".">
    <description>
        Builds jar for Xerces-For-Android
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init" description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac target="1.6" srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the .jar file -->
    <jar jarfile="${dist}/lib/Xerces-For-Android.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>
你知道我可能会错过什么吗?我不知道Java 7如何被引用,但它似乎是基于51.0的引用

另外两个旁注:

  • 我手动删除了.jar文件,并对Xerces For Android项目进行了清理,以确保没有旧的二进制文件
  • 我还对我的沙盒项目进行了清理,以测试jar文件的使用。同样的问题
  • 在卸载Java 7之后,我重新安装了Java 6u45。仍然无法通过51.0参考
  • 根据Joop Eggen的请求,我在eclipse中验证了编译器设置:
  • 解决方案:


    根据Joop Eggen的请求,我以详细模式运行ant脚本(
    ant-verbose>build.log
    )。。。这给了我更多的信息来确定问题所在,即
    ant
    的“clean”任务没有运行,这与eclipse中的“projectclean”不同。jar中的清单文件正在更新,但是类文件没有被重新创建,因为它们显示为“最新”。在将clean任务作为依赖项添加到compile任务之后,一切都按预期进行。在有人有类似问题的情况下离开此帖子。

    请确保在进行构建之前对所有内容进行彻底清理。major.Minor事件发生在Jar中的类上,而不一定发生在Jar本身上


    仔细检查并删除所有可以找到的.class文件,然后从头开始重新编译。

    在eclipse中禁用自动编译,或者最好从命令行运行ant。然后执行
    。检查是否有JAVA_HOME set和Ant安装

    Javac还具有
    编译器
    属性


    可能是偏执狂,检查.class文件,代码请参见。

    如果您使用maven构建项目,请参见下文。确保相应地指定源和目标。我在1.7中编译(保留了1.7)时遇到了这个错误,但在amazon beanstalk上发现它是1.6

    <build>
    <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <!-- http://maven.apache.org/plugins/maven-compiler-plugin/ -->
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>
    </plugins>
    </build>
    
    
    org.apache.maven.plugins
    maven编译器插件
    1.6
    1.6
    
    我更新了我的笔记,包括了关于此问题的疑难解答步骤。。。同样的问题。摘要可能重复:您不能在Java6JVM上运行为Java7编译的类。讨论中的类是jar中的类。正如文章所述,我正在编译jar并以版本6为目标。您的回答将我引向了核心问题,正如verbose命令一样,很明显ant clean任务没有运行,它在不同于eclipse clean的位置清理了文件。谢谢,这让我发疯了。
    <build>
    <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <!-- http://maven.apache.org/plugins/maven-compiler-plugin/ -->
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>
    </plugins>
    </build>