69

When I try to compile this class with javac, I get a compilation error and Test.class is not created.

public class Test { public static void main(String[] args) { int x = 1L; // <- this cannot compile } } 

But when I create this class in Eclipse, I can see that Test.class appears in target/classes. When I try to run this class from command line with java.exe, I get

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from long to int

Does Eclipse use its own special Java compiler to create a broken .class? How does java.exe know about complilation problems in .class?

1
  • 11
    +1. Nice question, I never thought about it :) Commented May 6, 2013 at 7:47

3 Answers 3

66

If you decompile your class file, you can see the below main() method. That is how the Java compiler knows about the compilation error in the class.

public static void main(String[] paramArrayOfString) { throw new Error("Unresolved compilation problem: \n\tType mismatch: cannot convert from long to int.\n"); } 

And all this happens because the compiler (Eclipse Compiler for Java), which Eclipse uses by default/comes bundled with, is not the same as the standard Java compiler!

Sign up to request clarification or add additional context in comments.

2 Comments

So, it doesn't use javac, then can someone compile a .java file in Eclipse after deleting the javac.exe file from the JDK kit? Just curious. :)
@ChaZ - Take a look at this question. Quoting a line from the answer there - 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. Hope that addresses your curiosity!:)
42

Eclipse uses the IBM compiler which has an option of creating classes which do not compile, replacing errors with

throw new Error(); 

IMHO, this is very bad practice and I have seen some very poor quality projects use this. The project didn't compile completely for weeks at a time.

Unlike fail fast strategies, which try to minimise the cost of bugs, discovering bugs as late as possible also maximises the cost of fixing them.

This strategy only works if you are writing prototype code quickly, i.e. code you know will never get into production. (It is hard to be sure this will be the case)

18 Comments

Sounds like death call to me. When writing a code, I often try, and I think most people should try, to make the code break at compile time, rather than letting it go as a bug and breaking at production. Now the obvious compile time error is allowed to be veiled, which can be killer/
I think in association with all that content assist, auto correction, highlighting, package explorer, ... Eclipse offers you almost can't miss bugs like that. From my point of view it's OK to test classes which don't compile, for instance after you implement an interface, you would have to write several useless returns to tests one single method... I think this would lead to dogged discussion, I just wanted to offer an alternative.
@FranzEbner There shouldn't be a chance you will be running code doesn't even compile. ;) IMHO, Mocking code is a better way to handle interfaces in tests.
The advantage of the Eclipse compiler allowing this, is that you can actually run existing code while writing new code (which doesn't compile yet) on your own computer.
@PeterLawrey I agree that deliberately shipping code that does not compile is a bad thing. Would the "very poor quality projects" you mention have benefittet from introducing a build robot?
|
27

Yes, Eclipse uses its own special compiler; known as "ecj". From Stack Overflow question What is the difference between javac and the Eclipse compiler?:

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.