如何eclipse执行java代码时有编译错误 [英] How eclipse execute java code when there are compile errors

查看:131
本文介绍了如何eclipse执行java代码时有编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在eclipse中执行下面的java代码时,它给出正确的输出(即打印'B类'),但是根据java规范,代码不能被编译(因为超类构造函数需要 int 参数,编译器提供给B类的构造函数包括调用超类没有arg构造函数,没有定义),当尝试使用javac命令编译文件线,它只编译超类,即A类,并失败,并出现以下编译错误:

When the following java code is executed in eclipse, it gives the correct output (i.e. prints 'Class B'), but according to java specification, the code cannot be compiled (since the super class constructor requires an int argument and the constructor provided by the compiler to class B includes a call to the super class no arg constructor, which is not defined), and when try to compile the file using javac command in command line, it only compiles the super class i.e. class A and fails with the following compile error:

B.java:8: cannot find symbol
symbol  : constructor A()
location: class A
public class B extends A {
   ^
1 error

以下是java文件的内容:

Following is the java file content:

class A {

    public A(int x) {
        //
    }
}

public class B extends A {

    public static void main(String[] args) {
        System.out.println("Class b");
    }
}

有人可以解释eclipse如何执行main方法能够编译类。提前感谢。

Can someone explain how eclipse executes the main method without bieng able to compile the class. Thanks in advance.

UPDATE

我删除了.class文件eclipse在bin文件夹中并试图执行该类,并且无法编译,因此没有运行。但是当我创建一个源文件与公共类名,并保存没有任何内容,然后添加上述内容,它的工作。如果eclipse使用以前编译的版本来执行,那么它如何给出 main 方法的输出,因为它没有 main c $ c>方法。)

I deleted the .class file creatd by eclipse in the bin folder and tried to execute the class, and it could not be compiled, therefore didn't run. But when I created a source file with the public class name and saved without any content, and then added the above content, it worked. If eclipse uses the previously compiled version to execute, how could it give the output of the main method, since it did not have the main method at the time of success full compilation?

推荐答案

c> ecj ),而不是 javac 。此编译器允许(部分)编译损坏的源文件

Eclipse uses its own Java compiler (called ecj) instead of javac. This compiler allows (partial) compilation of broken source files.

这篇关于如何eclipse执行java代码时有编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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