简体   繁体   English

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

[英]Eclipse error during compilation of a parameterized contructor code

When I write a simple parametrised constructor program, it compiles and runs in the command line. 当我编写一个简单的参数化构造函数程序时,它将在命令行中编译并运行。

However when it is executed within the Eclipse IDE, I received the following exception: 但是,当它在Eclipse IDE中执行时,我收到以下异常:

Exception in thread "main" java.lang.NoSuchMethodError: a_constructor.Test.(II)V at a_constructor.ParametrizedConstructor.main(ParametrizedConstructor.java:15). 线程“主”中的异常java.lang.NoSuchMethodError:a_constructor.ParametrizedConstructor.main(ParametrizedConstructor.java:15)上的a_constructor.Test。(II)V。

Code : 代码:

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

The ParametrizedConstructor code is clean and doesn't have any issues. ParametrizedConstructor代码很干净,没有任何问题。

Try: 尝试:

  • Delete the class files which were generated using the command prompt - If you are using the same location through eclipse to compile the same file. 删除使用命令提示符生成的类文件-如果通过eclipse使用相同的位置来编译相同的文件。
  • Make sure the Java Compiler and Java Build path was matched with the JDK versions. 确保Java编译器和Java Build路径与JDK版本匹配。

Alternate Solutions: 替代解决方案:

  • Try placing the code in Eclipse > Java Project > Under default package and run the file. 尝试将代码放在Eclipse> Java Project>在默认包下,然后运行文件。
  • We need to have the make sure that the compilation unit is matched to the class name ParametrizedConstructor.java (ie., public class) 我们需要确保编译单元与类名ParametrizedConstructor.java (即,公共类)匹配。

For References - Check also the below links for better understanding: 供参考-还要检查以下链接以更好地理解:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM