darrickc
darrickc

Reputation: 1932

Why are class files different size when compiling the same code in eclipse, and then with the eclipse compiler via ant?

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>

Upvotes: 6

Views: 5201

Answers (2)

Heisenbug
Heisenbug

Reputation: 484

Difference in sizes is because ANT's javac by default has debug mode as false.

Turning

debug="true"

in javac of ANT script will

  1. generate the same class file which is generated with maven or normal javac in cmd.
  2. because debug details such as line number and label nodes will also be added into the bytecode.

Upvotes: 0

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.

Upvotes: 5

Related Questions