如果Java源文件中存在语法错误,为什么Eclipse会生成.class文件? [英] Why does Eclipse generate .class file if there is a syntax error in my Java source file?

查看:83
本文介绍了如果Java源文件中存在语法错误,为什么Eclipse会生成.class文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Eclipse IDE创建项目时,即使我的代码中存在语法错误,它也会生成一个类文件吗?

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;
    }       
}

方法 firstCallToMethodFromTest 是我的Struts动作中的一种动作方法.Eclipse如何在源代码文件中存在语法错误的情况下为 Abc 类编译代码?

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?

推荐答案

有一个原因.它允许运行带有编译错误的应用程序(有点!).编译器所做的是为由于源代码错误而无法编译的任何方法创建存根方法.如果应用程序调用了这些存根方法之一,则会得到一个运行时异常,说明该方法存在编译错误.

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,这个功能" 主要是有害的 ... ...而且对于Eclipse新手来说可能非常令人困惑.但是,它对于想要在部分编写的类上进行测试等的人很有用.

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,运行"对话框中有一个复选框,可让您启用/禁用正在运行的具有编译错误的应用程序.(我总是禁用它!)

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

更新

此行为是Eclipse特有的.它由

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

这篇关于如果Java源文件中存在语法错误,为什么Eclipse会生成.class文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆