Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

eclipse

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

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?

like image 585
jeph perro Avatar asked Aug 21 '10 00:08

jeph perro


5 Answers

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 (i.e. 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).

like image 79
Vineet Reynolds Avatar answered Nov 13 '22 00:11

Vineet Reynolds


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.

like image 45
BalusC Avatar answered Nov 13 '22 02:11

BalusC


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.

like image 25
spbfox Avatar answered Nov 13 '22 01:11

spbfox


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.

like image 31
x4u Avatar answered Nov 13 '22 01:11

x4u


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.

like image 24
Viatcheslav Turkin Avatar answered Nov 13 '22 01:11

Viatcheslav Turkin