简体   繁体   中英

Eclipse won't compile, bad class file, wrong version

I am trying to compile code checked out of SVN from another developer. Eclipse has been giving me a lot of trouble lately.

Here are my project-specific settings: 替代文字

This is what the compile section of my ant file:

<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:

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. The classfile structure had changed in 1.5 due to the introduction of several new features and changes made in the Java Language Specification.

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 (ie 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.

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. This might be due to the jvm.dll from the JRE 1.4.2 installation being used to run Eclipse (and hence Ant).

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

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.

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.

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.

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.

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.

You can try to copy tools.jar from jdk/jre to common/lib of tomcat.

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. 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

Then run your build script issue will be resolved. :)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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