Java 初学者关于 main 方法中 String[] args 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2885160/
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
Java Beginner question about String[] args in the main method
提问by Serenity
So I just tried excluding String[]args from the mainmethod
所以我只是尝试String[]从main方法中排除args
It compiled alright !
它编译好了!
But JVM is showing an exception
但是JVM显示异常
Why did it compile when String[]args HAS to be included every time ?
为什么String[]每次都必须包含 args时它会编译?
What is going on here ? Why won't it show a compilation error ?
这里发生了什么 ?为什么不显示编译错误?
typing this made me think that may be compiler did not see it as THE mainmethod ..is that so ?
输入这个让我觉得可能是编译器没有将它视为main方法..是这样吗?
If that is the case..why not ? I mean isn't there supposed to be just one main method that MUST have String[]args as the argument ?
如果是这样的话..为什么不呢?我的意思是不应该只有一种必须以String[]args 作为参数的主要方法吗?
回答by tangens
The JVM is looking for a very special main method to run. Only the signature
JVM 正在寻找一种非常特殊的 main 方法来运行。只有签名
public static void main( String[] args )
is found. All other methods with name mainare just "normal" methods.
被发现。所有其他具有名称的方法main都只是“普通”方法。
回答by Michael Borgwardt
typing this made me think that may be compiler did not see it as THE main method ..is that so ?
输入这个让我觉得可能是编译器没有将它视为主要方法..是这样吗?
Correct. There is no compile error because you're perfectly free to have all kinds of methods named main. But when you start the JVM and give it a "main class", then it will look for a method static public void main(String[])in that class, and if it does not find such a method, it aborts with an exception.
正确的。没有编译错误,因为您可以完全自由地使用名为main. 但是当你启动 JVM 并给它一个“主类”时,它就会static public void main(String[])在那个类中寻找一个方法,如果没有找到这样的方法,它就会异常中止。
This allows you to have multiple main methods in your program and is really the only thing that makes sense if you think about it: Applications can be composed from classes and JAR files from lots of different sources, written by different people at different times, so in many cases you can't really have a single, designated "main class" right from the start.
这允许你在你的程序中有多个 main 方法,如果你仔细想想,这真的是唯一有意义的事情:应用程序可以由来自许多不同来源的类和 JAR 文件组成,由不同的人在不同的时间编写,所以在许多情况下,您不能从一开始就真正拥有一个指定的“主类”。
回答by Covar
Not every class has to have a public static void main(String[] args)method, only the one you want to run from the JVM. The result is that your class will compile with no error if it finds public static void main()but will throw an exception if you try and run it with the JVM because it can not find the entry point of the program.
不是每个类都必须有一个public static void main(String[] args)方法,只有一个你想从 JVM 运行的方法。结果是,如果找到您的类,它将在编译时没有错误,public static void main()但如果您尝试使用 JVM 运行它,则会抛出异常,因为它找不到程序的入口点。
Bottom line the entry point of your program must be public static void main(String[] args)which must be located in at least one of your .java files.
最重要的是public static void main(String[] args),程序的入口点必须位于至少一个 .java 文件中。
Note you can have multiple public static void main(String[] args)methods in your code (one per class) the advantage is you can use these to test and debug your classes individually.
请注意public static void main(String[] args),您的代码中可以有多个方法(每个类一个),优点是您可以使用这些方法单独测试和调试您的类。
回答by Affe
To try to answer "why is it legal to compile without a proper main method" it's because not every java project is a stand alone application that can be run. Some are just libraries, where other programs will include them as jar files and use their code, but they don't "run" themselves. Others may be web applications, where they are deployed onto a web server that has already been started, only the server itself actually has a proper "main" method. The web application project is opened up and executed by it.
试图回答“为什么在没有适当的 main 方法的情况下编译是合法的”是因为并非每个 java 项目都是可以运行的独立应用程序。有些只是库,其他程序会将它们包含为 jar 文件并使用它们的代码,但它们不会自己“运行”。其他可能是 web 应用程序,它们被部署到已经启动的 web 服务器上,只有服务器本身实际上有一个正确的“main”方法。Web 应用程序项目由它打开并执行。
The compiler didn't really know at compile time that you were intending to try to run your code as a stand along application.
编译器在编译时并不真正知道您打算尝试将代码作为独立应用程序运行。
回答by Powerlord
Java supports method overloading. This means you can have several methods with the same name, but different arguments.
Java 支持方法重载。这意味着您可以拥有多个名称相同但参数不同的方法。
Having said that, when you run java ClassName, Java looks in ClassName.classfor a method with the signature public static void main (String[])(it doesn't care what the String[]variable is named) and runs it. If it doesn't find one, Java will bomb out with the following exception:
话虽如此,当您运行 时java ClassName,Java 会查找ClassName.class带有签名的方法public static void main (String[])(它不关心String[]变量的名称)并运行它。如果它没有找到,Java 将爆炸并出现以下异常:
Exception in thread "main" java.lang.NoSuchMethodError: main
线程“main”中的异常 java.lang.NoSuchMethodError: main
回答by Eric Petroelje
You are correct. The runtime is looking for a main method that takes a string array as a parameter, and isn't finding it.
你是对的。运行时正在寻找一个将字符串数组作为参数的 main 方法,但没有找到它。
The fact that you have a main method that doesn't take a string array is irrelevant. just like any other method, you can create multiple versions of main()that take different parameters - the runtime will just ignore them.
您有一个不接受字符串数组的 main 方法这一事实无关紧要。就像任何其他方法一样,您可以创建main()采用不同参数的多个版本- 运行时将忽略它们。
回答by Michael
It's still a valid method. For example, you could have a static method called "main" with an int parameter if you wanted:
它仍然是一个有效的方法。例如,如果需要,您可以有一个名为“main”的静态方法,并带有一个 int 参数:
public static void main(int foo){}
The compiler doesn't complain because it's a valid method! It's just that, in order to run a Java program, Java looks for a static method called "main" with a single String array argument.
编译器不会抱怨,因为它是一个有效的方法!只是,为了运行 Java 程序,Java 会查找名为“main”的静态方法,并带有单个 String 数组参数。
回答by extraneon
Isn't that overloading? It's fully legal to define a method
这不是超载吗?定义一个方法是完全合法的
static void main() {
}
It's just not the entry point the JVM will be looking for.
它只是不是 JVM 将要寻找的入口点。
Overloading is the ability to have multiple methods with the same namebut different arguments. The compiler in fact creates a name based on the method name and the arguments.
重载是具有多个具有相同名称但不同参数的方法的能力。编译器实际上根据方法名称和参数创建了一个名称。
So a main(String[]) would be called, to the compiler, something like main_String_arr, and main() would be called something like main.
因此,编译器会调用 main(String[]) 之类的东西,例如 main_String_arr,而 main() 将被调用之类的东西。
回答by Betamoo
Yes..
是的..
The Java compiler will look for the same method signatureto consider it a main
Java 编译器将查找相同的方法签名以将其视为main
Writing any function that has the same name but another parameters will result in overloading functions..
Overloaded functions are not the same..!!
编写任何具有相同名称但另一个参数的函数将导致函数重载..
重载的函数不一样..!!
The case in C# is somehow different..
C# 中的情况有些不同。
At last, you must make sure that your main is like that:
最后,您必须确保您的主要内容是这样的:
public static void main(String[] args)
回答by Anton
You can have many methods named main, but only one can be THE main one - entry point to the program. It is the one with String[] args.
您可以有许多名为 main 的方法,但只有一个可以是主方法——程序的入口点。它是带有 String[] 参数的那个。

