206

Is Eclipse's Java compiler just a wrapper around the same core that the javac program is wrapped around, or is it a separate compiler altogether? If the latter, why would they reinvent the wheel?

sepp2k
  • 341,501
  • 49
  • 643
  • 658
Bart van Heukelom
  • 40,403
  • 57
  • 174
  • 291

4 Answers4

213

Eclipse has implemented its own compiler called as Eclipse Compiler for Java (ECJ).

It is different from the javac, the compiler that is shipped with Sun JDK. One notable difference is that the Eclipse compiler lets you run code that didn't actually properly compile. If the block of code with the error is never ran, your program will run fine. Otherwise, it will throw an exception indicating that you tried to run code that doesn't compile.

Another difference is that the Eclipse compiler allows for incremental builds from within the Eclipse IDE, that is, all code is compiled as soon as you finish typing.

The fact that Eclipse comes with its own compiler is also apparent because you can write, compile, and run Java code in Eclipse without even installing the Java SDK.

A few examples where ECJ is preferred over javac is:

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
jjnguy
  • 128,890
  • 51
  • 289
  • 321
  • Ok, but if you want to make a release build, which is better to use? – Bart van Heukelom Jun 17 '10 at 12:48
  • 3
    @Bart, the Eclipse compiler works well enough for enterprise release builds. – jjnguy Jun 17 '10 at 12:51
  • 3
    If you are compiling OSGi bundles instead of regular java, then the Eclipse compiler is more correct because it allows specifying package access rules. PDE will manage these access rules for you to match the OSGi runtime classloaders. https://bugs.eclipse.org/bugs/show_bug.cgi?id=106631 provides an example of problems that can happen without these rules. – Andrew Niefer Jun 17 '10 at 13:16
  • 7
    @jinguy I disagree that you should use Eclipse compiler for releases. As you stated in the answer, it can compile code with errors, you don't want stuff like public void foo() { throw new Error("Unresolved compilation problem: \n\tFOOBAR cannot be resolved\n"); } to appear in my production code. – Matthew Farwell Sep 28 '11 at 13:18
  • 11
    @Matthew Farwell He didnt say you should, but that you can. And if you ever create a build with errors in it, then something is wrong with your build process in the first place. – Stefan Nov 14 '11 at 22:28
  • 1
    JasperReports prefers ECJ over javac to compile reports if it's available. – Jarek Przygódzki Dec 17 '12 at 21:06
  • Also do see the next answer http://stackoverflow.com/a/3063001/632951 it has ***way*** more info. – Pacerier Aug 29 '14 at 07:19
  • No one mentioned Jikes? – Pacerier Nov 13 '14 at 19:53
  • 4
    Note that embedding ECJ in your application allows your program to run under a JRE instead of requiring a JDK. – Thorbjørn Ravn Andersen Jun 04 '15 at 08:07
  • 7
    @MatthewFarwell to close a loop here: for release builds you're advised to simply _not_ specify the compiler argument `-proceedOnError` and it will simply not produce .class files from source with errors. – Stephan Herrmann Aug 01 '15 at 19:58
  • i see JDT, what is JDT? https://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2FresAdv_builders.htm – Alexander Mills Feb 13 '19 at 01:50
36

Everyone has already explained that they're different. Here are some difference in behaviors I've noticed between the two compilers. They all boil down to a bug in (at least) one of the implementations.

Compile-time optimization related

Generics type inferrence related

Community
  • 1
  • 1
polygenelubricants
  • 348,637
  • 121
  • 546
  • 611
  • 1
    Actually I knew about this difference after a long night: Eclipse was reporting an error about something that to me seemed legal (I don't remember what), in my desperation (I barely could stay awake) I just feed the code to javac and then it worked smoothly! I found in Google that I had to upgrade the JDT in order to get the fix for that issue. – Abel Morelos Jun 18 '10 at 15:18
  • 5
    I've found a number of differences between the compilers handling of generics in difficult cases. Here are two I made questions about on here in case you want to add them to your answer: http://stackoverflow.com/questions/13501836/possible-java-compiler-bug-program-does-not-compile-with-some-compilers http://stackoverflow.com/questions/13980552/oracle-jdk-and-eclipse-jdt-compilers-disagree-which-is-compiling-this-incorrect – Elias Vasylenko Mar 18 '13 at 10:13
  • 5
    Anonymous classes are never static according to the JLS but they can be declared in static scope. When using reflection to ask if such a class is static, ECJ's generated code says no while javac's [says yes](http://ideone.com/cGgs58). Related post [here](http://stackoverflow.com/questions/758570/is-it-possible-to-make-anonymous-inner-classes-in-java-static). – Paul Bellora Apr 18 '13 at 14:30
  • 2
    Any semantical difference in the emitted bytecode is a bug in either implementation. This is not very interesting in my opinion. I can easily produce a long list of such "differences" by just listing the open bugs from javac and ecj. – aioobe Oct 17 '14 at 19:37
  • FYI, Netbeans doesn't suffer from any such "differences" since it uses javac's internal API to do everything that EJC does. – Aleksandr Dubinsky May 19 '15 at 13:16
  • Oddly enough, I came here for a precisely opposite reason? I have an instance where the Eclipse compiler balks at a faulty line of code with Generics, but javac lets it go. (JDK1.8) – Steve Cohen Dec 06 '17 at 22:54
18

Eclipse's built-in compiler is based on IBM's Jikes java compiler. (Note that Eclipse also started its life at IBM). It is completely independent of Sun's Java compiler in the JDK; it is not a wrapper around Sun's javac.

Jikes has existed for a long time, it used to be a lot faster than the standard JDK Java compiler (but I don't know if that's still true). As to why IBM wanted to write their own Java compiler: maybe because of licensing reasons (they also have their own Java implementation).

Jesper
  • 186,095
  • 42
  • 296
  • 332
  • 32
    They didn't *really* write their own Java compiler. Eclipse has a long lineage back to Visual Age for Smalltalk, before Java even existed. Since the two languages are actually somewhat similar, they simply adapted their existing technology. Sun's compiler is also completely unsuitable for use in an IDE, *especially* in an incremental Smalltalk-style IDE like the original Visual Age for Java since it always wants to compile whole files. IBM's compiler can incrementally compile only the fragments that have changed. It can even compile snippets that aren't even legal Java, which is used in the – Jörg W Mittag Jun 17 '10 at 14:35
  • 2
    Eclipse scrapbook where you can simply write snippets of code, highlight them and run them, without having to put them into a class, a main method, or even into a method *at all*. – Jörg W Mittag Jun 17 '10 at 14:36
  • 1
    @JörgWMittag Actually, javac's internal API (as used by Netbeans) can be used to achieve all of the same goals. – Aleksandr Dubinsky May 19 '15 at 13:15
  • 2
    @AleksandrDubinsky: How well did that actually work in 1997, when Visual Age for Java was released? – Jörg W Mittag Feb 03 '19 at 10:22
15

It is a separate compiler altogether. This is needed as javac doesn't allow compilation of slightly broken code, from the eclipse site

An incremental Java compiler. Implemented as an Eclipse builder, it is based on technology evolved from VisualAge for Java compiler. In particular, it allows to run and debug code which still contains unresolved errors.

Matthew Farwell
  • 58,043
  • 17
  • 119
  • 166
BenM
  • 3,976
  • 2
  • 22
  • 26
  • 1
    Why would you want the the compilation of "slightly" broken code? – Steve Cohen Dec 06 '17 at 22:52
  • 7
    @SteveCohen: Because you want the compiler to provide syntax highlighting, semantic highlighting, refactoring support, type checking, code completion, hints, and all the other things a compiler does while you are writing your code, and while you are writing your code, it is more or less by definition incomplete (otherwise, why are you still writing it?) An IDE that only works at the very end of the project, when everything has already been implemented, would be quite useless. – Jörg W Mittag Feb 03 '19 at 10:25