Search code examples
javaconstructornosuchmethoderrorparameterized-constructor

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:

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

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

Solution

  • The ParametrizedConstructor code is clean and doesn't have any issues.

    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.
    • Make sure the Java Compiler and Java Build path was matched with the JDK versions.

    Alternate Solutions:

    • Try placing the code in Eclipse > Java Project > Under default package and run the file.
    • We need to have the make sure that the compilation unit is matched to the class name ParametrizedConstructor.java (i.e.., public class)

    For References - Check also the below links for better understanding: