Java 程序中是否必须需要 Main 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2896322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:13:12  来源:igfitidea点击:

Is the Main method must needed in a Java program?

java

提问by Praveen

Is the main method needed to write a java program?

编写java程序需要main方法吗?

This is my code:

这是我的代码:

package example;

public class HelloWorld {

    public HelloWorld() {

    }

    public String getHelloWorld() {

        return "Hello From Java!";
    }
}

It shows an error at compilation:

它在编译时显示错误:

java.lang.NoSuchMethodError: main
Exception in thread "main"

采纳答案by emory

The main method is not needed in java programs. As others have pointed out, web applications do not use the main method.

java程序中不需要main方法。正如其他人指出的那样,Web 应用程序不使用 main 方法。

It is not even needed in standalone applications. Consider

在独立应用程序中甚至不需要它。考虑

class JavaAppWithoutMain
{
    static
    {
    System . out . println ( "Hello World!" ) ;
    }
}

I compiled it and ran and obtained the following result:

我编译并运行,得到如下结果:

Hello World!
Exception in thread "main" java.lang.NoSuchMethodError: main

For standalone applications you must either have

对于独立应用程序,您必须具有

  1. a main method or
  2. a static initializer.
  1. 主要方法或
  2. 静态初始化程序。

Main is preferred.

主要是首选。

回答by GustyWind

No, it is not needed for e.g. web applications. They do not use a main()method, but if you are testing or running a stand-alone application, to know what output you are expecting, you might require a main()method.

不,例如网络应用程序不需要它。它们不使用main()方法,但如果您正在测试或运行独立应用程序,要了解您期望的输出,您可能需要一个main()方法。

回答by David M

The mainmethod is the default entry point for a programme. If you don't define one, and then try to execute the jar produced, this is what you'll see. If you're not trying to produce a programme that needs launching independently, you won't need it - for example, a jar referenced by other programmes, or a website.

main方法是程序的默认入口点。如果你不定义一个,然后尝试执行生成的 jar,这就是你会看到的。如果您不想制作需要独立启动的程序,则不需要它——例如,其他程序引用的 jar 或网站。

回答by Andrew Hare

Without a mainmethod you application will have no entry point. Yes, it is required for any executable program.

如果没有main方法,您的应用程序将没有入口点。是的,任何可执行程序都需要它。

回答by Andrey

Standalone applications require main, because it is entry-point. How will JVM know where to start program?

独立应用程序需要 main,因为它是入口点。JVM 如何知道从哪里开始程序?

回答by James P.

The reason why you're getting this error message is because you're attempting to run a class using java (java.exe on Windows) and it's expecting to find a main() method.

您收到此错误消息的原因是您正在尝试使用 java(Windows 上的 java.exe)运行一个类,并且它希望找到一个 main() 方法。

This method isn't required as such but it can form an entry point where an application is initiated. You can rewrite your class as follows to achieve the result you were seeking:

此方法本身不是必需的,但它可以形成启动应用程序的入口点。您可以按如下方式重写您的课程以获得您想要的结果:

package example;
public class HelloWorld {

 // Running a class using java invokes this method
 public static void main(String[] args) {
  HelloWorld hw = new HelloWorld();

  System.out.println( hw.getHelloWorld() );
 }

 public HelloWorld() { 
 }

 public String getHelloWorld() {
  return "Hello From Java!";
 }
}

回答by Pascal Thivent

If you try to execute a Java class, the JVM will look for a mainmethod to invoke it. From the CHAPTER 12 Executionof the Java Language Specification:

如果您尝试执行 Java 类,JVM 将寻找main调用它的方法。来自第 12 章Java 语言规范的执行

A Java virtual machine starts up by loading a specified class and then invoking the method mainin this specified class. Section §12.1outlines the loading, linking, and initialization steps involved in executing main, as an introduction to the concepts in this chapter. Further sections specify the details of loading (§12.2), linking (§12.3), and initialization (§12.4).

Java 虚拟机通过加载指定的类然后调用main该指定类中的方法来启动。第12.1节 概述了执行中涉及的加载、链接和初始化步骤main,作为本章概念的介绍。进一步的部分指定了加载(§12.2)、链接(§12.3)和初始化(§12.4) 的细节

Not all classes need a main, only the one that serve as "entry point" for execution.

并不是所有的类都需要一个main,只有一个作为执行的“入口点”。

回答by user377488

Your java application or program (not every single class) needs atleast one main method to run it. And the one you have got is not a compilation error but a run time error.

您的 Java 应用程序或程序(不是每个类)至少需要一个 main 方法来运行它。你得到的不是编译错误而是运行时错误。

回答by shubham mishra

"When you save program with the name same as the class name which contain main() method, then at the time of execution the JVM will create a object of that class and with that object JVM will call the main() metod as object.main().

“当您使用与包含 main() 方法的类名相同的名称保存程序时,在执行时 JVM 将创建该类的对象,并使用该对象 JVM 将调用 main() 方法作为对象。主要的()。

So if main() method is missing( static initializer is also missing ) then it will throw an exception."

因此,如果缺少 main() 方法(也缺少静态初始值设定项),那么它将抛出异常。”

For web application same explanation as above.

对于 Web 应用程序,解释与上述相同。

ref: Java Understanding Java main method on logic

参考:Java 理解 Java 逻辑上的主要方法