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.

Goutham
  • 37
  • 5
  • 2
    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`. – Elliott Frisch May 31 '23 at 15:22
  • Additionally, I think the class containing `main` should be `public` – QBrute May 31 '23 at 15:32
  • @QBrute not required. – rzwitserloot May 31 '23 at 15:38
  • Using eclipse or intelij? – S14321K May 31 '23 at 16:02
  • Does this answer your question? [Error: Selection does not contain a main type](https://stackoverflow.com/questions/16225177/error-selection-does-not-contain-a-main-type) – S14321K May 31 '23 at 16:18
  • @ElliottFrisch - it is not pointless, i'm just a beginner and learning the implementation with some basic examples. I cannot comment on others capabilities ... – Goutham May 31 '23 at 17:26
  • @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 snippet – Goutham May 31 '23 at 17:28

2 Answers2

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

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Goutham
  • 37
  • 5
-1

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

For Eclipse https://stackoverflow.com/a/75637946/11962586

UPDATE - Try this way

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

S14321K
  • 220
  • 3
  • 19
  • If it's a dupe mark it as a dupe; if it isn't, link-only answers aren't great. – Dave Newton May 31 '23 at 16:06
  • @Dave Newton , I couldn't see the duplicate option. – S14321K May 31 '23 at 16:12
  • 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)*. – Dave Newton May 31 '23 at 16:15
  • I tried running this code. works good. The problem is with IDE. Could you please tell me how to mark it as duplicate? – S14321K May 31 '23 at 16:17
  • 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. – Dave Newton May 31 '23 at 16:19
  • Thanks for assisting. I raised request to close. – S14321K May 31 '23 at 16:21
  • No problem :thumbs-up: – Dave Newton May 31 '23 at 16:21
  • @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 me – Goutham May 31 '23 at 18:08
  • @Goutham The dupe comments were directed at the poster of this answer. – Dave Newton May 31 '23 at 18:17
  • @Goutham you got those privileges now. Have fun. I faced the same. We need more upvotes to access those privileges. – S14321K Jun 01 '23 at 05:25