Java Eclipse 无法编译,类文件错误,版本错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3535722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eclipse won't compile, bad class file, wrong version
提问by jeph perro
I am trying to compile code checked out of SVN from another developer. Eclipse has been giving me a lot of trouble lately.
我正在尝试编译从另一个开发人员处检出的 SVN 代码。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?
这个错误是什么意思?
采纳答案by Vineet Reynolds
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. The classfile structure had changed in 1.5 due to the introduction of several new features and changes made in the Java Language Specification.
49.0 的类文件版本属于 Java 1.5.x,而 48.0 的类文件版本属于 Java 1.4.x 版本之一。由于引入了几个新特性以及 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. 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 (i.e. JDK home).
从错误中,可以推断出预期的是 Java 1.4 类文件,而找到的是 Java 1.5 类文件。编译器似乎是 Java 1.4 编译器,因此您应该尝试验证是否使用了正确版本的 Java 编译器以及正确的 JDK(即 JDK home)。
ADDENDUM
附录
Ant tends to look for the javac executable in the $JAVA_HOME/bin/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.
Ant 倾向于在 $JAVA_HOME/bin/javac 中查找 javac 可执行文件。如果 JAVA_HOME 环境变量设置不正确,比如 Java 1.4 home,那么即使在 Eclipse 中也有可能出现所描述的错误。
ADDENDUM #2
附录#2
The addition of entries to the PATH environment variable could resultin altering the search classpath behavior of Ant, possibly resulting in a different tools.jar being used for the purpose of compiling the sources. This might be due to the jvm.dll from the JRE 1.4.2 installation being used to run Eclipse (and hence Ant).
向 PATH 环境变量添加条目可能会改变 Ant 的搜索类路径行为,可能会导致使用不同的 tools.jar 来编译源代码。这可能是由于 JRE 1.4.2 安装中的 jvm.dll 被用于运行 Eclipse(以及 Ant)。
回答by BalusC
bad class file: 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
错误的类文件:C:\Program Files\Java\jre1.6.0_07\lib\rt.jar(java/io/IOException.class)
类文件有错误的 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. 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.
可以看出,上述类文件是使用生成 49.0 版本类文件的编译器编译的,而 ant 期望使用生成 48.0 版本类文件的编译器编译它。由于有问题的类是 JRE 的一部分,因此您需要更新类路径build.xml
以包含包含版本 48.0.0 的类文件的 JRE。
回答by x4u
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. 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.
您似乎在使用 1.5 版 java 编译器运行 Ant,但针对 1.6 安装的类库 (rt.jar) 进行编译。您应该设置 build.xml,以便它始终使用相同 Java 版本的(编译器和类库)。
回答by spbfox
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...". In the popup dialog navigate to jvm tab and check which jre is used to run your script.
要查找在哪个 jvm ant 下运行,请在 Eclipse 中右键单击您的构建脚本并选择“Run as...”。在弹出对话框中导航到 jvm 选项卡并检查使用哪个 jre 来运行您的脚本。
回答by Yuri.Bulkin
Maybe you need to use JDK JRE which is located at C:\Program Files\Java\jdk\jre, not public
也许您需要使用位于 C:\Program Files\Java\jdk\jre 的 JDK JRE,而不是 public
回答by PAco
回答by Viatcheslav Turkin
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. 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. To switch to correct tools.jar in eclipse open Window>Preferences>Ant>Runtime>Global Entries.
该错误消息意味着您的 Ant 正在使用的 tool.jar 中包含的编译器工具的版本比它尝试编译的类要旧。消息“[javac] class file has wrong version 50.0 should be 49.0”意味着您的类文件需要 jdk 6.0 但您的 tool.jar 来自 jdk 5.0 发行版。要在 Eclipse 中切换到正确的 tools.jar,请打开 Window>Preferences>Ant>Runtime>Global Entries。
回答by Amit Thakur
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. To fix this issue i your Eclipse Go to 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
您还需要更改 ANT 的 Java 版本,因为如果您将运行时环境用作 JDK1.8,那么您的 ANT 也应该指向相同的版本,或者 Ant 应该引用 JDK1.8/lib/tools 中的 tools.jar 文件。罐子位置。要在您的 Eclipse 中解决此问题,请转到 Window --> Preference --> Ant -->Runtime -->Global Entries 。在这里检查并使用最新版本更新 tools.jar 路径如下:C:\Program Files\Java\jdk1.8.0_60\lib\tools.jar
Then run your build script issue will be resolved. :)
然后运行您的构建脚本问题将得到解决。:)
回答by Arvind
Changing the tools.jar in eclipse to the correct version fixed it. I was using jre 1.6 and had to change it to use 1.8 to get the class file in version 52.
将 eclipse 中的 tools.jar 更改为正确的版本即可修复它。我使用的是 jre 1.6 并且不得不将其更改为使用 1.8 以获取版本 52 中的类文件。