What's the point of String[] args in Java?

2019-02-20 21:12发布

Whenever you declare the main method in a class, you always have to do a String array called "args". What's the point? Unless I live under a rock, command line agruments in Java are barely used anymore. And when I try and run this...

//this program won't compile
public class SomeClass {
    public static void main(){
        System.out.println("This text will never be displayed :(");
    }
}

The output shows this in red text:

Error: Main method not found in class SomeClass, please define the main method as:
    public static void main(String[] args)

I, the newbie Java programmer, would greatly appreciate it if anyone told my why it's required to enter that parameter into a main method.

7条回答
家丑人穷心不美
2楼-- · 2019-02-20 21:41

Short answer: because that's the way Java is.

Command-line arguments are used all the time, but you don't always see them due to launcher scripts, or because the program's running on a server, etc.

That said, a lot of time the command line arguments are of the -D variety, slurped up by the JVM before reaching main. But it depends on what you're doing.

查看更多
3楼-- · 2019-02-20 21:46

A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched. (From Command-Line Arguments) and as everyone else said here, it is the way it is!

For gods sake,Please Don't say if I don't need this ,no-one else need this! :)

查看更多
成全新的幸福
4楼-- · 2019-02-20 21:48

One case I can think of is, when you want to have a command line driven interface for your software along with the GUI. An example is the Android tools, all of them have console driven interfaces.

查看更多
再贱就再见
5楼-- · 2019-02-20 21:50

Because that is the signature of the main method that is called when you execute a Java class. There needs to be some convention which method will be executed. By convention it is the

public static void main(String[] args) method

And yes, you do live under the rock, there are plenty of situations when command line arguments are used. Why would they not be used?

You could ask: why require it? Why not just pick any other main method? The answer is that it would be adding complexity with 0 benefit. As is now, main function looks distinctive. If you look at it, you know it is the one that will get called. If any main would be called, you would have to always ask yourself: is the main I am looking at the one to be invoked, or is there another main in this class which takes precedence?

查看更多
手持菜刀,她持情操
6楼-- · 2019-02-20 21:54

Because

  1. Command-line arguments are still used, even by many UI programs (did you know that Microsoft Outlook supports some very handy command-line arguments?)*; and:
  2. That's Just How Java Works (TM). Among other things, it reduces the complexity of both the code (by disallowing multiple forms and possible accidental shadowing) and run-time (by not needing to find out "which main" to call). Allowing a secondary form without "args" just adds too little.

Happy coding...


*Yes, Outlook is not Java. However, if Outlook has command-line arguments, well, they must still be worth something -- it was a hyperbole ;-)

Almost every UI program that deals opening reading files will allow specifying which file to open via command-line arguments (Gimp, Notepad, Firefox, to name a few others). Among other things, this is to allow integration with "double clicking to open" on items in Windows Explorer and similar.

查看更多
可以哭但决不认输i
7楼-- · 2019-02-20 21:54

I actually have no idea why it's required, other than to say that it is a syntactical convention. In the same way that a function is (defined function-name()) in Lisp/Scheme, or there are do..end blocks in Ruby, the syntax of a Java Main function is with String[] args.

As for not using command line arguments, it's entirely dependent on the program. Entirely. I write programs in java all the time that take command line arguments; it's just a question of what you're trying to accomplish.

查看更多
登录 后发表回答