英文:
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)
英文:
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 )
答案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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论