简体   繁体   English

即使我已经声明了它,也找不到 Main 方法

[英]Main method not found even if I've declared it

I want to create a simple java class, with a main method, but when I compile my code, I get this error message :我想用 main 方法创建一个简单的 java 类,但是当我编译我的代码时,我收到以下错误消息:

Error: Main method not found in class errors.TestErrors, please define the main method as: public static void main(String[] args)错误:在类 errors.TestErrors 中找不到 Main 方法,请将 main 方法定义为:public static void main(String[] args)

This is the source code :这是源代码:

package errors;

public class TestErrors {
    public static void main(String[] args){
        System.out.println("hello");
    }
}

Why I'm seeing this error, as you can notice I've alreader declared the main method !为什么我看到这个错误,你可以注意到我已经声明了 main 方法!

As said in my comments, looks like you've declared a String class among your own classes.正如我在评论中所说,看起来您已经在自己的类中声明了一个String类。 To prove this, I've created a basic example:为了证明这一点,我创建了一个基本示例:

class String {
}

public class CarelessMain {
    public static void main(String[] args) {
        System.out.println("won't get printed");
    }
    public static void main(java.lang.String[] args) {
        System.out.println("worked");
    }
}

If you execute this code, it will print "worked" in the console.如果您执行此代码,它将在控制台中打印"worked" If you comment the second main method, the application will throw an error with this message (similar for your environment):如果您注释第二个main方法,应用程序将抛出一条错误消息(与您的环境类似):

Error: Main method not found in class edu.home.poc.component.CarelessMain, please define the main method as:错误:在 edu.home.poc.component.CarelessMain 类中找不到 Main 方法,请将 main 方法定义为:

 public static void main(String[] args)

This usually happens if ur complete project isnotconfigured correctly or one of your class in project has still some errors in such cases IDE will prompt stating the same that project contains some error and you still proceed (ie run your class) as project has some bugs new classes will not be created and IDE will run the class which was available previously如果您的完整项目未正确配置,或者您的项目中的一个类仍然存在一些错误,则通常会发生这种情况,在这种情况下,IDE 将提示说明该项目包含一些错误并且您仍然继续(即运行您的类),因为项目有一些新的错误不会创建类,IDE 将运行以前可用的类

to make sure this is ur case u can add new class in your project and try to run it and if ur getting no such class exist then there its is a perfect evidence为了确保这是你的情况,你可以在你的项目中添加新类并尝试运行它,如果你没有这样的类存在,那么它就是一个完美的证据

Just check your java file, it has not been saved.只需检查您的java文件,它尚未保存。 Please save all java files before compiling.编译前请保存所有java文件。

If the answers above are not working for you: make sure "main" is not capitalized in your method definition.如果上述答案对您不起作用:请确保您的方法定义中的“main”没有大写。

OK: public static void main(String[] args) OK: public static void main(String[] args)

ERROR: public static void Main(String[] args)错误: public static void Main(String[] args)

Though the error message makes the required syntax for the main method clear, incorrect caps can be hard to spot.尽管错误消息明确了 main 方法所需的语法,但很难发现不正确的大写字母。 It took me ~30 minutes of troubleshooting to find this typo today.今天我花了大约 30 分钟的故障排除时间才找到这个错字。 This was clearly not an issue for the OP, but is another easy/likely way to produce the same error message, and this answer may help other users.这显然不是 OP 的问题,而是产生相同错误消息的另一种简单/可能的方法,这个答案可能会帮助其他用户。

I had this issue just now.我刚才有这个问题。 This error came up because the JRE file that i switched out didn't had the full library.出现此错误是因为我切换出的 JRE 文件没有完整的库。 After I corrected/added the right JRE system library the problem went away.在我更正/添加了正确的 JRE 系统库后,问题就消失了。

Right click on the class name (which you are trying to run)->Run As->Run Configurations->Under Main Class Tab右键单击类名(您正在尝试运行)-> 运行方式-> 运行配置-> 在主类选项卡下

Write your main class name and hit on Run.写下你的主要类名并点击运行。

尝试注释第一行。

//package errors;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JAR 没有找到主要清单,即使在 XML 中声明它也是如此 - No main manifest found by JAR, even when declared it in XML 主要类无法找到或加载甚至强硬有一个主要方法 - main class could not be found or loaded even tough there is a main method 即使我在依赖项中添加了compile方法,也无法解析FileUtils - Cannot resolve FileUtils even if I've added compile method in dependencies Java 变量不在 scope 中,即使我已经在 class 中声明了它? - Java variable is out of scope even though I've already declared it in the class? 如何从另一个方法访问在主方法中实例化的对象? - How do I access an object I've instantiated in my main method from another method? Intellij Idea-即使声明了main方法,也无法运行简单的Java类 - Intellij Idea - can't run a simple java class even though the main method is declared 为什么在 static 块中声明的变量在 main 方法中使用时找不到? - Why does a variable declared in static block when used in main method is not not found? 我已经编写了一个线程,但是如何将其添加到主方法中? - I've written a thread but how do I add it to my main method? 为什么即使我已经扩展了类,我也无法访问受保护的 java 方法? - Why can't I access protected java method even thought I've extended the class? 即使使用正确的主签名也无法在Windows 10上找到Java主要方法错误 - Java main method not found error on Windows 10 even with correct main signature
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM