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?

huangapple go评论73阅读模式
英文:

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?

问题

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

我正在尝试执行这段代码,并遇到以下错误:

"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"

但我已经定义了main方法,并尝试检查是否存在任何语法和拼写错误,但代码仍然返回错误。

英文:
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.

答案1

得分: 1

谢谢你的帮助。

我觉得我找到了问题所在。在同一个包中有两个文件

> 包 code.ninja;

> Main.java
> Deriveded.java
  • Main.java(这个文件有一个类名为 Derived),所以当我尝试创建一个名为 Derived.java 的新Java类时,它会报错,但当我将类名更改为 Deriveded.java 时,错误消失了。
  • Deriveded.java(之前是Derived.java)

同一包中的Main.java程序

Deriveded.java

英文:

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

答案2

得分: -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

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?
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?
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?

英文:

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

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?
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?
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?

huangapple
  • 本文由 发表于 2023年5月31日 23:18:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定