Why am I getting the error 'Main method not found in class' even though I defined the main method in my Java program with inheritance? [duplicate]
Asked Answered
P

2

1
package code.now;


class Test {
    public void fun() {
        System.out.println("Coding Ninjas");
    }
}

class Derived extends Test {
    
    public void fun() {
        System.out.println("Coding Ninjas");
    }
    public static void main(String[] args) {
        Derived obj = new Derived();
        obj.fun();
    }
}

i'm trying to execute this code and running into the below error

"Error: Main method not found in class code.ninja.Derived, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application"

but I already defined the main method and also tried to check for any syntax and spelling mistakes but still the code is returning the error.

Parton answered 31/5, 2023 at 15:18 Comment(7)
It looks like you have put both classes in one file. Don't do that. Derived goes in Derived.java and Test goes in Test.java. Also, you currently overload the single function from Test in Derived (and just duplicated the code when you did so). That is both confusing and pointless. Also your declared package is code.now but your error message is from code.ninja.Gulley
Additionally, I think the class containing main should be publicPistareen
@Pistareen not required.Sympathize
Using eclipse or intelij?Honniball
Does this answer your question? Error: Selection does not contain a main typeHonniball
@ElliottFrisch - it is not pointless, i'm just a beginner and learning the implementation with some basic examples. I cannot comment on others capabilities ...Parton
@ElliottFrisch you are right about the package name mismatch in the error and the code. I might have made a typo in writing the package name in code snippetParton
H
-1

I faced the same before. Your code is good. No issues. Problem with IDE config.

For Eclipse https://mcmap.net/q/264590/-error-selection-does-not-contain-a-main-type

UPDATE - Try this way

enter image description here enter image description here enter image description here

Honniball answered 31/5, 2023 at 16:3 Comment(10)
If it's a dupe mark it as a dupe; if it isn't, link-only answers aren't great.Deliquescence
@Dave Newton , I couldn't see the duplicate option.Honniball
I didn't downvote, but: 1) we don't know if it's the right answer because we don't know how the OP is running the code, 2) duplicates are the first option listed when we vote to close a question, and 3) link-only answers are not how SO works (and if the answer is an SO link it's a dupe by definition).Deliquescence
I tried running this code. works good. The problem is with IDE. Could you please tell me how to mark it as duplicate?Honniball
You're missing what I'm saying: we don't know if the OP is even using an IDE (or which one if they are). Underneath the question are links: Share Edit Follow Close Flag. Click "Close" to close the question (a vote to close, actually). Find the duplicate question from the list provided, by searching, or by pasting a link. Click the "Vote to Close" button.Deliquescence
Thanks for assisting. I raised request to close.Honniball
No problem :thumbs-up:Deliquescence
@DaveNewton , the reason i posted a new question cause the answers in the suggested questions didn’t fix my problem. Neither could i comment cause I don't have enough privileges to comment on others answers. So the only logical option for me is to post a new question as the answers on the other questions didn't help meParton
@Parton The dupe comments were directed at the poster of this answer.Deliquescence
@Parton you got those privileges now. Have fun. I faced the same. We need more upvotes to access those privileges.Honniball
P
1

Thanks for the help.

I think I found the issue. There are two files in the same package

package code.ninja;

> Main.java
> Deriveded.java
  • Main.java ( this has a class name Derived ), so when I tried to create a new java class with Derived.java it's throwing error, but after I changed the class to Deriveded.java, the errors are gone.
  • Deriveded.java (previously it was Derived.java )

Main.java program in the same package

Deriveded.java

Parton answered 31/5, 2023 at 17:47 Comment(0)
H
-1

I faced the same before. Your code is good. No issues. Problem with IDE config.

For Eclipse https://mcmap.net/q/264590/-error-selection-does-not-contain-a-main-type

UPDATE - Try this way

enter image description here enter image description here enter image description here

Honniball answered 31/5, 2023 at 16:3 Comment(10)
If it's a dupe mark it as a dupe; if it isn't, link-only answers aren't great.Deliquescence
@Dave Newton , I couldn't see the duplicate option.Honniball
I didn't downvote, but: 1) we don't know if it's the right answer because we don't know how the OP is running the code, 2) duplicates are the first option listed when we vote to close a question, and 3) link-only answers are not how SO works (and if the answer is an SO link it's a dupe by definition).Deliquescence
I tried running this code. works good. The problem is with IDE. Could you please tell me how to mark it as duplicate?Honniball
You're missing what I'm saying: we don't know if the OP is even using an IDE (or which one if they are). Underneath the question are links: Share Edit Follow Close Flag. Click "Close" to close the question (a vote to close, actually). Find the duplicate question from the list provided, by searching, or by pasting a link. Click the "Vote to Close" button.Deliquescence
Thanks for assisting. I raised request to close.Honniball
No problem :thumbs-up:Deliquescence
@DaveNewton , the reason i posted a new question cause the answers in the suggested questions didn’t fix my problem. Neither could i comment cause I don't have enough privileges to comment on others answers. So the only logical option for me is to post a new question as the answers on the other questions didn't help meParton
@Parton The dupe comments were directed at the poster of this answer.Deliquescence
@Parton you got those privileges now. Have fun. I faced the same. We need more upvotes to access those privileges.Honniball

© 2022 - 2025 — McMap. All rights reserved.