javaeclipsecompiler-errors

How can Eclipse create a class with unresolved compilation problems?


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?


Solution

  • 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!