问题
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?
回答1:
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.
回答2:
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.
回答3:
It is an Eclipse specific feature called Incremental Java compilation.
It is a part of JDT Core
. JDT Core is the Java infrastructure of the Java IDE.
An incremental Java compiler
: Implemented as an Eclipse builder, it is based on technology evolved from VisualAge for Java compiler.In particular, it allows to run and debug code which still contains unresolved errors.
and that is why you can see the compiler .class 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 a part of your execution flow. None the less when the jvm will try to execute the method having an error jvm will simply shut down terminating your program.
来源:https://stackoverflow.com/questions/18349168/why-does-eclipse-generate-class-file-if-there-is-a-syntax-error-in-my-java-sour