Souvik Pal
Souvik Pal

Reputation: 63

error: can't find main(String[]) method in class: Animal

class Animal
{  
    public void getName(String name)
    {
        System.out.println("Name of Animal : "+name);
    } 
}
class Dog extends Animal
{  
    public void getBreed(String breedName)
    {
        System.out.println("Breed Name : "+breedName);
    } 
}
class Program16
{  
    public static void main(String args[])
    {  
        Dog d=new Dog();  
        d.getName("Tommy");
        d.getBreed("Labrador"); 
    }
}

Output

D:\JavaCollege>javac Program16.java

D:\JavaCollege>java Program16.java

error: can't find main(String[]) method in class: Animal

Upvotes: 0

Views: 1598

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1504052

The code it fine1 - it's the way you're running it that's the problem.

Instead of this:

java Program16.java

Run this:

java Program16

That tells the java command which class you want to run. You've already compiled the source files (.java) into class files (.class) so now you just want the JVM to execute the main method in the Program16 class.


1 The way you're specifying the type of the args parameter is unconventional, but valid. It would be more conventional to write it as String[] args - the syntax with [] at the end of the name was introduced for compatibility with other languages, but is generally regarded as less readable than keeping all the type information together.

Upvotes: 5

Related Questions