编译参数化构造函数代码期间出现 Eclipse 错误

Eclipse error during compilation of a parameterized contructor code

当我编写一个简单的参数化构造函数程序时,它在命令行中编译并运行。

然而,当它在 Eclipse IDE 中执行时,我收到以下异常:

Exception in thread "main" java.lang.NoSuchMethodError: a_constructor.Test.(II)V at a_constructor.ParametrizedConstructor.main(ParametrizedConstructor.java:15).

代码:

//write a java program which listed the concept of parameterized constructor
class Test {
    int a,b;
    Test (int x, int y) {
        a=x;
        b=y;
        System.out.println("========================");
        System.out.println("value of a=" +a);
        System.out.println("value of b=" +b);
        System.out.println("========================");
    }
}
public class ParametrizedConstructor {
    public static void main(String[] args) {
        Test t1=new Test(10,20);
        Test t2=new Test(100,200);
        Test t3=new Test(1000,2000);
    }
}

ParametrizedConstructor 代码很干净,没有任何问题。

尝试:

  • 删除使用命令提示符生成的 class 个文件 - 如果您通过 eclipse 使用相同的位置来编译相同的文件。
  • 确保 Java 编译器和 Java 构建路径与 JDK 版本匹配。

替代解决方案:

  • 尝试将代码放在 Eclipse > Java 项目 > 默认包和 运行 文件中。
  • 我们需要确保编译单元与 class 名称 ParametrizedConstructor.java 相匹配(即 public class)

参考 - 另请查看以下链接以更好地理解:

  • 可能Causes of 'java.lang.NoSuchMethodError: main Exception in thread "main"'