How can Eclipse create a class with unresolved compilation problems?

JavaEclipseCompiler Errors

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

This 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");
}

If you decompile your class file, you can see the above main() method of the class file, which the compiler has generated. This is because of the compiler which Eclipse uses (Eclipse Compiler for Java) is not the same as the standard Java compiler!

Solution 2 - Java

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)

Solution 3 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEvgeniy DorofeevView Question on Stackoverflow
Solution 1 - JavaRahulView Answer on Stackoverflow
Solution 2 - JavaPeter LawreyView Answer on Stackoverflow
Solution 3 - JavaErich SchubertView Answer on Stackoverflow