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

When I create my project using the Eclipse IDE, does it generate a class file, even if 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 ie source code error return result; } } 

The firstCallToMethodFromTest method firstCallToMethodFromTest called as an action method from my Struts action. How does Eclipse allow you to compile code for the Abc class where there are syntax errors in my source code file?

+6
source share
3 answers

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

IMO, this “feature” is mostly harmful ... and it can be very confusing for newcomers to Eclipse. However, it may be useful for people who want to perform runtests, etc. In partially written classes.

IIRC, in the Run dialog box there is a checkbox that allows you to enable / disable running applications with compilation errors. (I always turned it off!)

UPDATE

This behavior is specific to Eclipse. It is controlled by the parameter in the settings panel "Window> Preferences> Run / Debug> Run".

+9
source

Since you can run and debug a class that was partially compiled, as long as you only move parts of the code that were compiled without errors. If your control flow arrives with a compilation error, an exception will occur at runtime.

Just remember, if you ever changed the code directly during debugging (replacing the hot code): many IDEs even warn you that under certain conditions you partially delete the existing code, but you still want to continue this exact debugging session, so this function really necessary.

+4
source

This is a special feature of Eclipse called Java Incremental Compilation .

This is part of the JDT Core . JDT Core is the Java framework for the Java IDE.

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

and therefore you can see .class compiler files.

 But how can i run a partially compiled code ? 

You can run it as long as the method that has the error is not part of the execution thread. However, when jvm tries to execute the method with a jvm error, you just stop terminating your program.

+2
source

Source: https://habr.com/ru/post/952172/


All Articles