Java 中 String[] args 有什么意义?
每当您在类中声明 main 方法时,您始终必须创建一个名为“args”的 String 数组。有什么意义?除非我生活在岩石下,否则 Java 中的命令行参数几乎不再使用。当我尝试运行这个...
//this program won't compile
public class SomeClass {
public static void main(){
System.out.println("This text will never be displayed :(");
}
}
输出以红色文本显示:
错误:在SomeClass类中找不到Main方法,请将main方法定义为:
public static void main(String[] args)
作为新手 Java 程序员,如果有人告诉我为什么需要输入该参数,我将不胜感激进入一个主要方法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
因为这是执行 Java 类时调用的 main 方法的签名。需要有一些约定来执行哪个方法。按照惯例,它是 是
的,您确实生活在岩石下,有很多情况需要使用命令行参数。为什么不使用它们?
你可能会问:为什么需要它?为什么不直接选择其他主要方法呢?答案是,这会增加复杂性,但收益为零。现在看来,main 函数看起来很独特。如果你看一下它,你就知道它将被调用。如果要调用任何 main 函数,您必须始终问自己:我正在查看的 main 函数是要调用的 main 函数,还是该类中是否有另一个 main 函数优先?
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
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?
简短的回答:因为 Java 就是这样。
命令行参数一直在使用,但由于启动器脚本或程序在服务器上运行等原因,您并不总是看到它们。
也就是说,很多时候命令行参数都是
-D
变体,在到达main
之前被 JVM 吸收。但这取决于你在做什么。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 reachingmain
. But it depends on what you're doing.Java 应用程序可以接受来自命令行的任意数量的参数。 这允许用户在启动应用程序时指定配置信息。(来自 命令行参数),正如其他人在这里所说的,事情就是这样!
看在上帝的份上,请不要说我不需要这个,没有其他人需要这个! :)
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! :)
因为
快乐编码...
*是的,Outlook 不是 Java。但是,如果 Outlook 有命令行参数,那么它们肯定还是有价值的 - 这是夸张的说法 ;-)
几乎每个处理打开阅读文件的 UI 程序都将允许指定要打开的文件通过命令行参数(Gimp、Notepad、Firefox 等)。除此之外,这是为了允许与 Windows 资源管理器和类似项目中的“双击打开”集成。
Because
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.
实际上我不知道为什么需要它,只是说这是一个语法约定。就像 Lisp/Scheme 中的函数 (define function-name()) 或 Ruby 中的 do..end 块一样,Java Main 函数的语法是使用 String[] args。
至于不使用命令行参数,完全取决于程序。完全。我一直用 java 编写带有命令行参数的程序;这只是一个你想要实现什么目标的问题。
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.
我能想到的一种情况是,当您想要为您的软件提供一个命令行驱动的界面以及 GUI 时。一个例子是 Android 工具,它们都有控制台驱动的界面。
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.
命令行参数支持在很大程度上是编程语言的标准。即使在这个 GUI 时代,也存在各种隐藏的方法来使用命令行参数运行程序。我知道 Windows 为高级用户提供了快捷方式配置,例如,您可以使用一组给定的命令行参数运行程序。
Java 还强制执行类型,并通过扩展函数签名(如果您不知道这些是什么,请在 Google 上查找)。 main 函数应采用字符串数组 - 如果您定义的 main 函数与该参数签名(1 个参数,字符串数组)不匹配,则会导致不兼容。
Java 支持函数重载(您可以使用不同的参数多次定义相同的函数名)。为了找到要调用的函数,Java 采用输入参数类型并查找采用匹配参数的适用定义函数。
当程序运行时,Java 会专门查找名为 main 且带有 1 个参数(String [])的函数。您的程序没有使用该参数规范定义主函数,因此此查找失败并显示错误消息。
Command-line argument support is largely standard among programming languages. Even in this age of GUIs, there are all sorts of hidden ways to run a program with command line arguments. I know Windows has shortcut configurations for advanced users where you can run a program with a given set of command line arguments, for example.
Java also enforces types, and by extension function signatures (look it up on Google if you don't know what those are). The main function is expected to take an array of Strings - if the main function you define doesn't match that argument signature (1 argument, array of Strings), then it causes an incompatibility.
Java supports function overloading (you can define the same function name several times with different arguments). To find which function to invoke, Java takes the input argument types and looks for an applicable defined function that takes the matching arguments.
When the program runs, Java specifically looks for a function named main with 1 argument (String []). Your program doesn't define a main function with that argument specification, so this lookup fails with an error message.