Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Eclipse generate .class file if there is a syntax error in my Java source file?

Tags:

java

eclipse

When I am creating my project using the Eclipse IDE it is generating a class file even when there is a syntax error in my code?

class Test {    
    public void test(String value) {
        System.out.println("TEST CALLED WITH VALUE " + value);
    }
}

class Abc {
    Test obj = new Test();      
    public String firstCallToMethodFromTest() {
        System.out.println("FIRST CALL TO THE METHOD FROM TEST CLASS");
        String result = obj.test("TEST");
        return result;
    }

    public String secondCallToMethodFromTest() {
        System.out.println("SECOND CALL TO THE METHOD FROM TEST CLASS");
        String result = obj.test(); 
        // There is no such method in test class i.e source code error
        return result;
    }       
}

Method firstCallToMethodFromTest is called as an action method from my Struts action. How does Eclipse make it possible to compile code for the Abc class where there are syntax errors in my source code file?

like image 856
User2228 Avatar asked Aug 21 '13 04:08

User2228


People also ask

Why .class file is created in Java?

class" file is created as a result of successful compilation by the Java compiler from the ". java" file. Each class in the . java file is compiled into a separate class file if the ".

How do I convert .class to .Java in Eclipse?

Click on Window > Preferences > Java > Decompiler to configure which default class decompiler to decompile Java class.

How does a .class file differ from a .java file?

A . java file contains your Java source code while a . class file contains the Java bytecode produced by the Java compiler.


2 Answers

There is a reason. It allows applications with compilation errors to be run (sort of!). What the compiler does is to create stub methods for any methods that it cannot compile due to errors in the source code. If the application calls one of these stub methods, you get a runtime exception that says that the method had a compilation error.

IMO, this "feature" is mostly harmful ... and it can be very confusing for Eclipse newbies. However, it can be useful for people who want to runtests, etc on partly written classes.

IIRC, there is a checkbox in the Run dialogs that allows you to enable / disabling running applications that have compilation errors. (I always disable it!)

UPDATE

This behaviour is Eclipse specific. It is controlled by a setting in the "Window > Preferences > Run/Debug > Launching" preference panel.

like image 174
Stephen C Avatar answered Oct 31 '22 17:10

Stephen C


Because you can run and debug a class that has only been compiled partially, as long as you only move through the code parts which compiled without error. If your control flow comes to the place with the compilation error, an exception will happen at runtime.

Just remember if you have ever changed code directly during debugging (hot code replacement): Many IDEs will even warn you that under some conditions you are partially removing existing code, but you still want to continue that exact same debugging session, so this feature is really needed.

like image 39
Bananeweizen Avatar answered Oct 31 '22 17:10

Bananeweizen