简体   繁体   English

如果我的Java源文件中存在语法错误,为什么Eclipse会生成.class文件?

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

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? 当我使用Eclipse IDE创建项目时,即使我的代码中存在语法错误,它也会生成一个类文件?

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. 方法firstCallToMethodFromTest被我的Struts动作调用为动作方法。 How does Eclipse make it possible to compile code for the Abc class where there are syntax errors in my source code file? Eclipse如何为我的源代码文件中存在语法错误的Abc类编译代码?

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. IMO,这个“功能” 大多是有害的 ......对于Eclipse新手而言,它可能会非常混乱。 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. IIRC,“运行”对话框中有一个复选框,允许您启用/禁用运行具有编译错误的应用程序。 (I always disable it!) (我总是禁用它!)

UPDATE UPDATE

This behaviour is Eclipse specific. 此行为是特定于Eclipse的。 It is controlled by a setting in the "Window > Preferences > Run/Debug > Launching" preference panel. 它是由一个受控设置在“窗口>首选项>运行/调试>启动”偏好面板。

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. 请记住,如果您在调试期间直接更改了代码(热代码替换):许多IDE甚至会警告您,在某些情况下,您正在部分删除现有代码,但您仍希望继续完全相同的调试会话,因此此功能是真的需要。

It is an Eclipse specific feature called Incremental Java compilation . 它是一个名为Incremental Java编译的Eclipse特定功能。

It is a part of JDT Core . 它是JDT Core的一部分。 JDT Core is the Java infrastructure of the Java IDE. JDT Core是Java IDE的Java基础结构。

  • An incremental Java compiler : Implemented as an Eclipse builder, it is based on technology evolved from VisualAge for Java compiler. An incremental Java compiler :作为Eclipse构建An incremental Java compiler实现,它基于从VisualAge for Java编译器演化而来的技术。 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. 这就是为什么你可以看到编译器.class文件。

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. 然而,当jvm尝试执行具有错误的方法时,jvm只会关闭终止程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java错误:源文件错误:文件不包含类 - Java Error: bad source file: file does not contain class 为什么我的日食不生成R.java文件? - Why does my eclipse does not produce the R.java file? 为什么我的Java代码在eclipse中运行,但不能作为.exe运行。 或.class文件 - why does my java code run in eclipse but not as an .exe. or .class file 为什么我的 Java 文件在 Eclipse 中看起来很奇怪? - Why Is My Java File Looks Strange in Eclipse? 为什么Antlr不生成lexer Java文件? - Why does Antlr not generate a lexer java file? 为什么此Java代码在一个Eclipse项目中而不在另一个Eclipse项目中生成错误? - Why does this Java code generate an error in one Eclipse project but not the other? Why does Android generate a Java class from my Kotlin class - Why does Android generate a Java class from my Kotlin class 为什么我不能在Eclipse Maven项目中将类导入到我的java文件中? - Why can't I import a class into my java file in my Eclipse Maven Project? Java 错误 - 错误的源文件:文件不包含类 x 。 请删除或确保它出现 - Java error - bad source file: file does not contain class x . Please remove or make sure it appears Java Annotation Processor:仅在不存在时生成源文件 - Java Annotation Processor: only generate a source file if it does not exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM