tags:

views:

67

answers:

1

I am making an automated build environment using ant to build a freshly checked out source tree using the same eclipse compiler that is used in eclipse. The problem is that some of the resulting class files are different in size than the class file generated by compiling within eclipse. Why is this? Is this ok, and to be expected? As prescribed I'm telling Ant to use the eclipse compiler, like:

<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
        <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
        <antcall target="build"/>
</target>
+4  A: 

Eclipse uses its own compiler, which generates slightly different - but correct - bytecode.

Ant uses the standard Sun compiler - javac - available in the JDK.

The eclipse compiler can be downloaded from eclipse.org and ant told to use it. This has the added benefit of being able to compile with the JRE alone, which is much easier to install than the full JDK. Look for "JDT Core Batch Compiler" in http://download.eclipse.org/eclipse/downloads/drops/R-3.6-201006080911/index.php


EDIT: Even with the same compiler the byte code generated may be different. Some factors that influence on this are:

  • Target JVM - Java 6 byte codes are slightly different than Java 1.2 byte codes.
  • Optimization level (some inlining, better left to the JVM these days)
  • Debug information inclusion.
Thorbjørn Ravn Andersen
"using the same eclipse compiler that is used in eclipse" (though my guess would be the compiler flags aren't the same)
Jefromi
The command line interface to ecj intentionally mimics those for javac.
Thorbjørn Ravn Andersen
Good explanation although I'd always pull down the full JDK anyway, not harder just takes a little longer.
Bill K
I added the "compile with eclipse compiler" target that I am using (which was recommended somewhere).
Why does Eclipse have its own compiler?
Aaron F.
Also, the Eclipse compiler is good. Really good. Any time I've seen the Eclipse compiler differ non-trivially from javac, Eclipse has either been more conformant to the spec or equally conformant but more smart or forgiving.
Mark Peters
@Aaron F: An application like Eclipse (with JDT) needs to have a VERY good suite of static analysis tools to support the feedback that it does via syntax highlighting, error highlighting, suggestions, auto-complete, etc. Javac doesn't provide this support through an API so they made their own suite. At that point creating a compiler is very little extra effort and has the added benefit of not needing to separately install the JDK.
Mark Peters
@Aaron F, when you have used Eclipse a lot you will see that it knows really much about your Java code. To learn that, the compiler needs to tell the editor all it needs to know, so the Eclipse project essentially _had_ to write their own compiler to do so. They just packaged it up so it can be used standalone too.
Thorbjørn Ravn Andersen
@darrick, can be different options and Java version targets.
Thorbjørn Ravn Andersen
a downvote? Wonder why...
Thorbjørn Ravn Andersen
I gave you a down vote because in the question I specify that I am using the same compiler as eclipse, but in your answer you said that the difference is that ant and eclipse uses different compilers. Your answer - though interesting, and well written - doesn't answer my question.