简体   繁体   中英

Exception in thread “main” java.lang.NoSuchMethodError: main - how to fix it?

Please help me to fix my simple program. I'm just newbie of Java Programming. When i write the program, and the program has been success compile but can't run cause the massage error "Exception in thread "main" java.lang.NoSuchMethodError: main" has been show. Everybody can help me to run this program ?

The coding is:

Person class...

//Program to display student details using inheritance
class Person {
    String name;
    int age;
}

Student class...

class Student extends Person
{
    int mark1,mark2,mark3;
    void putdata()
    {
        System.out.println("Name = " + name);
        System.out.println("Age = " + age);
        System.out.println("Mark1 = " + mark1);
        System.out.println("Mark2 = " + mark2);
        System.out.println("Mark3 = " + mark3);
    }
 }

Marks class...

class Marks
{
    public static void main(String[] args)
    {
        Student obj1=new Student();
        obj1.name="Sultanah";
        obj1.age=17;
        obj1.mark1=67;
        obj1.mark2=87;
        obj1.mark3=97;
        obj1.putdata();
    }
}

JVM when try to launch the program looks for the class with the same name as the name passed to run the program. Once the class is found then it looks for the main method in it. So make sure that you are running the program using Mark as the class name because that is the class that contains your main method.

Either:

  1. Your main() method shoudl be placed in the Student class, if that's the class you're executing, or

  2. You should be executing the class that does contain the main() method, ie Marks .

It seems you trying to run class that doesn't have main method. according to your code Marks.java has main method so after compilation you can run Marks class using

java Marks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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