Java 命令行参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/716153/
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 Command line arguments
提问by burntsugar
I am trying to detect whether the 'a' was entered as the first string argument.
我试图检测 'a' 是否作为第一个字符串参数输入。
采纳答案by Bj?rn
public class YourClass {
public static void main(String[] args) {
if (args.length > 0 && args[0].equals("a")){
//...
}
}
}
回答by Joachim Sauer
Your main
method has a String[]
argument. That contain the arguments that have been passed to your applications (it's often called args
, but that's not a requirement).
你的main
方法有一个String[]
论据。其中包含已传递给您的应用程序的参数(通常称为args
,但这不是必需的)。
回答by Rob
Command-line arguments are passed in the first String[]
parameter to main()
, e.g.
命令行参数在第一个String[]
参数中传递给main()
,例如
public static void main( String[] args ) {
}
In the example above, args
contains all the command-line arguments.
在上面的示例中,args
包含所有命令行参数。
The short, sweet answer to the question posed is:
对所提出问题的简短而甜蜜的回答是:
public static void main( String[] args ) {
if( args.length > 0 && args[0].equals( "a" ) ) {
// first argument is "a"
} else {
// oh noes!?
}
}
回答by Jakub Arnold
Command line arguments are accessible via String[] args
parameter of main
method.
命令行参数是通过可访问String[] args
的参数main
的方法。
For first argument you can check args[0]
对于第一个参数,您可以检查 args[0]
entire code would look like
整个代码看起来像
public static void main(String[] args) {
if ("a".equals(args[0])) {
// do something
}
}
回答by Bill the Lizard
Every Java program starts with
每个 Java 程序都以
public static void main(String[] args) {
That array of type String
that main()
takes as a parameter holds the command line arguments to your program. If the user runs your program as
类型的数组String
的是main()
作为一个参数保持命令行参数给程序。如果用户运行你的程序
$ java myProgram a
then args[0]
will hold the String "a".
然后args[0]
将保存字符串“a”。
回答by TofuBeer
As everyone else has said... the .equals method is what you need.
正如其他人所说...... .equals 方法正是您所需要的。
In the off chance you used something like:
万一你使用了类似的东西:
if(argv[0] == "a")
then it does not work because == compares the location of the two objects (physical equality) rather than the contents (logical equality).
那么它不起作用,因为 == 比较两个对象的位置(物理相等)而不是内容(逻辑相等)。
Since "a" from the command line and "a" in the source for your program are allocated in two different places the == cannot be used. You have to use the equals method which will check to see that both strings have the same characters.
由于命令行中的“a”和程序源中的“a”分配在两个不同的位置,因此无法使用 ==。您必须使用 equals 方法来检查两个字符串是否具有相同的字符。
Another note... "a" == "a" will work in many cases, because Strings are special in Java, but 99.99999999999999% of the time you want to use .equals.
另一个注意事项... "a" == "a" 在很多情况下都可以使用,因为字符串在 Java 中是特殊的,但是在 99.99999999999999% 的情况下您想使用 .equals。
回答by John Ellinwood
Use the apache commons cliif you plan on extending that past a single arg.
如果您计划将其扩展到单个 arg 之后,请使用apache commons cli。
"The Apache Commons CLI library provides an API for parsing command line options passed to programs. It's also able to print help messages detailing the options available for a command line tool."
“Apache Commons CLI 库提供了一个 API,用于解析传递给程序的命令行选项。它还能够打印详细说明命令行工具可用选项的帮助消息。”
Commons CLI supports different types of options:
Commons CLI 支持不同类型的选项:
- POSIX like options (ie. tar -zxvf foo.tar.gz)
- GNU like long options (ie. du --human-readable --max-depth=1)
- Java like properties (ie. java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo)
- Short options with value attached (ie. gcc -O2 foo.c)
- long options with single hyphen (ie. ant -projecthelp)
- POSIX 之类的选项(即 tar -zxvf foo.tar.gz)
- GNU 喜欢长选项(即 du --human-readable --max-depth=1)
- Java 类属性(即 java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo)
- 带有附加值的短选项(即 gcc -O2 foo.c)
- 带有单个连字符的长选项(即 ant -projecthelp)
回答by user2885596
Try to pass value a and compare using the equals method like this:
尝试传递值 a 并使用 equals 方法进行比较,如下所示:
public static void main(String str[]) {
boolean b = str[0].equals("a");
System.out.println(b);
}
Follow this link to know more about Command line argument in Java
按照此链接了解有关Java 中命令行参数的更多信息
回答by Prudhvi Bellam
Command line arguments are stored as strings in the String
array String[] args that is passed to
main()`.
命令行参数作为字符串存储在String
数组String[] args that is passed to
main()` 中。
java [program name] [arg1,arg2 ,..]
Command line arguments are the inputs that accept from the command prompt while running the program. The arguments passed can be anything. Which is stored in the args[]
array.
命令行参数是在运行程序时从命令提示符接受的输入。传递的参数可以是任何东西。哪个存储在args[]
数组中。
//Display all command line information
class ArgDemo{
public static void main(String args[]){
System.out.println("there are "+args.length+"command-line arguments.");
for(int i=0;i<args.length;i++)
System.out.println("args["+i+"]:"+args[i]);
}
}
Example:
例子:
java Argdemo one two
The output will be:
输出将是:
there are 2 command line arguments:
they are:
arg[0]:one
arg[1]:two