简体   繁体   English

Eclipse不会编译,坏的类文件,错误的版本

[英]Eclipse won't compile, bad class file, wrong version

I am trying to compile code checked out of SVN from another developer. 我正在尝试编译从其他开发人员检出的SVN代码。 Eclipse has been giving me a lot of trouble lately. Eclipse最近给我带来了很多麻烦。

Here are my project-specific settings: 以下是我的项目特定设置: 替代文字

This is what the compile section of my ant file: 这是我的ant文件的编译部分:

<target name="compile" depends="build-common, init" description="Compile files. ">
    <javac srcdir="${src_dir}" destdir="${build_dir}" debug="true" >
        <classpath path="${tomcat_home}/lib/servlet-api.jar;" />
    </javac>
</target>

When I compile ( using Ant ) I get an error message: 当我编译(使用Ant)时,我收到一条错误消息:

compile:
    [javac] Compiling 3 source files to H:\MYCOMPANY\portlets\build
    [javac] H:\MYCOMPANY\portlets\src\com\mycompany\portlets\CourseList.java:3: cannot access java.io.IOException
    [javac] bad class file: C:\Program Files\Java\jre1.6.0_07\lib\rt.jar(java/io/IOException.class)
    [javac] class file has wrong version 49.0, should be 48.0
    [javac] Please remove or make sure it appears in the correct subdirectory of the classpath.
    [javac] import java.io.IOException;
    [javac]                ^
    [javac] 1 error

What does this error mean? 这个错误是什么意思?

The class file version of 49.0 belongs to Java 1.5.x, whereas the one of 48.0 belongs to one of the Java 1.4.x version. 49.0的类文件版本属于Java 1.5.x,而48.0的类文件属于Java 1.4.x版本之一。 The classfile structure had changed in 1.5 due to the introduction of several new features and changes made in the Java Language Specification. 由于Java语言规范中引入了一些新功能和更改,因此类文件结构在1.5中已更改。

From the error, one can deduce that a Java 1.4 classfile was expected, whereas a Java 1.5 classfile was found. 从错误中,可以推断出Java 1.4类文件是预期的,而找到了Java 1.5类文件。 It appears that the compiler is a Java 1.4 compiler, so you should attempt to verify whether you're using the right version of the Java compiler and also the right JDK (ie JDK home). 看起来编译器是Java 1.4编译器,因此您应该尝试验证您是使用正确版本的Java编译器还是正确的JDK(即JDK home)。

ADDENDUM 附录

Ant tends to look for the javac executable in the $JAVA_HOME/bin/javac . Ant倾向于在$ JAVA_HOME / bin / javac中查找javac可执行文件。 If the JAVA_HOME environment variable has been set incorrectly, say to a Java 1.4 home, then there is a likelihood of getting the described error even in Eclipse. 如果JAVA_HOME环境变量设置不正确,比如说是Java 1.4主页,那么即使在Eclipse中也有可能出现所描述的错误。

ADDENDUM #2 ADDENDUM#2

The addition of entries to the PATH environment variable could result in altering the search classpath behavior of Ant, possibly resulting in a different tools.jar being used for the purpose of compiling the sources. 向PATH环境变量添加条目可能会导致更改Ant的搜索类路径行为,从而可能导致使用不同的tools.jar来编译源代码。 This might be due to the jvm.dll from the JRE 1.4.2 installation being used to run Eclipse (and hence Ant). 这可能是由于JRE 1.4.2安装中的jvm.dll用于运行Eclipse(以及Ant)。

bad class file: C:\\Program Files\\Java\\jre1.6.0_07\\lib\\rt.jar(java/io/IOException.class) 坏类文件:C:\\ Program Files \\ Java \\ jre1.6.0_07 \\ lib \\ rt.jar(java / io / IOException.class)
class file has wrong version 49.0, should be 48.0 类文件有错误的版本49.0,应该是48.0

It is telling that the mentioned class file is been compiled using a compiler which generates class files of version 49.0, while ant expects it to be compiled using a compiler which generates class files of version 48.0. 它告诉所提到的类文件是使用生成49.0版本的类文件的编译器编译的,而ant期望使用生成版本48.0的类文件的编译器进行编译。 Since the class in question is part of the JRE, you need to update the classpath in your build.xml to include the JRE containing class files of version 48.0. 由于有问题的类是JRE的一部分,因此您需要更新build.xml的类路径以包含包含版本48.0的类文件的JRE。

First of all your project settings have nothing to do with your build script. 首先,您的项目设置与构建脚本无关。 Project settings tell you how Eclipse builds your project, not how your ant file will run. 项目设置告诉您Eclipse如何构建项目,而不是如何运行ant文件。

To find under which jvm ant runs, right-mouse click your build script in Eclipse and choose "Run as...". 要查找jvm ant运行的位置,请在Eclipse中右键单击构建脚本,然后选择“Run as ...”。 In the popup dialog navigate to jvm tab and check which jre is used to run your script. 在弹出的对话框中,导航到jvm选项卡​​并检查用于运行脚本的jre。

The error message means that the compiler tool included in tool.jar your Ant is using is of older version than the class it attempts to compile. 错误消息表示您的Ant使用的tool.jar中包含的编译器工具是旧版本,而不是它尝试编译的类。 Message "[javac] class file has wrong version 50.0 should be 49.0" would mean that your class file needs jdk 6.0 but your tool.jar comes from jdk 5.0 distribution. 消息“[javac]类文件有错误的版本50.0应该是49.0”意味着你的类文件需要jdk 6.0但你的tool.jar来自jdk 5.0发行版。 To switch to correct tools.jar in eclipse open Window>Preferences>Ant>Runtime>Global Entries. 要在eclipse中切换到正确的tools.jar,请打开Window> Preferences> Ant> Runtime> Global Entries。

You seem to be running Ant with a version 1.5 java compiler but compiling against the class libraries (rt.jar) of a 1.6 installation. 您似乎使用1.5版Java编译器运行Ant,但是针对1.6安装的类库(rt.jar)进行编译。 You should set up your build.xml so that it will consistently use both (the compiler and the class libraries) of the the same java version. 您应该设置build.xml,以便它始终使用相同java版本的(编译器和类库)。

You can try to copy tools.jar from jdk/jre to common/lib of tomcat. 您可以尝试将tools.jarjdk/jre复制到tomcat的common/lib

It worked for me as demonstrated here 它的工作对我来说这表现在这里

You need to change the Java Version for ANT also because If you are using your Run Time Environment as JDK1.8 then Your ANT should be also point to same version or Ant should refer tools.jar file from JDK1.8/lib/tools.jar location. 您还需要更改ANT的Java版本,因为如果您将运行时环境用作JDK1.8,那么您的ANT也应该指向相同的版本,或者Ant应该引用JDK1.8 / lib / tools中的tools.jar文件。罐子的位置。 To fix this issue i your Eclipse Go to Window --> Preference --> Ant -->Runtime -->Global Entries . 为了解决这个问题,我的Eclipse转到Window - > Preference - > Ant - > Runtime - > Global Entries。 Here check and update tools.jar path with latest version as below.: C:\\Program Files\\Java\\jdk1.8.0_60\\lib\\tools.jar 在这里检查并更新具有最新版本的tools.jar路径如下:C:\\ Program Files \\ Java \\ jdk1.8.0_60 \\ lib \\ tools.jar

Then run your build script issue will be resolved. 然后运行您的构建脚本问题将得到解决。 :) :)

Changing the tools.jar in eclipse to the correct version fixed it. 将eclipse中的tools.jar更改为正确的版本修复它。 I was using jre 1.6 and had to change it to use 1.8 to get the class file in version 52. 我使用jre 1.6并且必须将其更改为使用1.8来获取版本52中的类文件。

也许您需要使用位于C:\\ Program Files \\ Java \\ jdk \\ jre的JDK JRE,而不是公共

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM