tags:

views:

263

answers:

5

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?

+3  A: 

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.

BalusC
Umm, Java 6 has classfile version of 50.0.
Vineet Reynolds
@Vineet: Hmm, you're right. I was just translating from the error message. But that doesn't explain why `IOException.class` in JRE 1.6 has version 49.0 as stated in the error message? Anyway, I generified my answer to remove the specific JRE versions.
BalusC
@BalusC, good catch. Version 49.0 in a 1.6 JRE does not make sense.
Vineet Reynolds
@BalusC - Hmm, interesting...I checked classes in both the 1.5 and 1.6 JRE `rt.jar` (including ones that were specifically updated in 1.6) and they all have version 49. I wonder what the purpose in that is..
Tim Stone
+2  A: 

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.

x4u
This might be the problem, where do I specify that? In the ant build, should I add target="1.5" source="1.5" to the compile command?
jeph perro
It's in your "External Tools Configurations", i.e. per ant launcher. Especially check the JRE and Classpath tabs. The problem is is probably in the JRE setting, but you may have manually added a different version of the rt.jar to the Classpath.
Stroboskop
Wonder how they managed to _do_ that?
Thorbjørn Ravn Andersen
+5  A: 

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

Vineet Reynolds
How do I check the Ant compiler version?
jeph perro
Well, by default this tends to be the version of compiler in the JDK home (the Eclipse launch configuration can help identify the home). You could specify a different compiler using the executable attribute of the javac Ant task: http://ant.apache.org/manual/Tasks/javac.html
Vineet Reynolds
I don't understand, I am using Java 1.6 JDK. I added a screenshot to my post. Why should that matter?
jeph perro
You're using JDK 1.6 to call ant, but the compiler is called by ant in a new process which may use a different JDK
seanizer
JAVA_HOME is set correctly to C:\Program Files\Java\jdk1.6.0_07 however, Oracle Dev Suite has added some jre 1.4 values to my PATH environment variable.
jeph perro
+2  A: 

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.

spbfox
This helped a little. There is no JVM tab, but the JRE tab does correctly use jre1.6.0_07.
jeph perro
Sorry, I did not have Eclipse opened and gave wrong tab name. Well, next I would check:1. If you have jre 1.4 installed anywhere.2. If you have JAVA_HOME or JRE_HOME defined, and if yes where they point to.3. Run java -version from command line to see which java your path points to. If it is 1.4, adjust your path environment to point to 1.6.
spbfox
A: 

Maybe you need to use JDK JRE which is located at C:\Program Files\Java\jdk\jre, not public

Yuri.Bulkin