Java Beginner question about String[] args in the

2019-04-12 00:49发布

So I just tried excluding String[] args from the main method

It compiled alright !

But JVM is showing an exception

Why did it compile when String[] args HAS to be included every time ?

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 main method ..is that so ?

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 ?

标签: java main
13条回答
放荡不羁爱自由
2楼-- · 2019-04-12 01:34

You can use the compiler to compile part of an application: e.g. to make a jar file which you will call from another part of the app; or an applet that is started in a different way. Therefore the compiler cannot complain about the lack of a main(String[]) method. But trying to run the results of such a compile.

When you try to run the results of such a compile java is always looking for a specific main(String[]); if it can't find it it will throw a runtime exception. The main used to start an app must have exactly that signature.

查看更多
登录 后发表回答