简体   繁体   English

命令行参数显示0?

[英]Command line arguments show 0?

package commandLine;

public class commandLine {

public static void main(String[] args) {
    System.out.println("There are " +args.length+ " Command-line Arguments");
    System.out.println("They are: ");
    for(int i=0;i<args.length;i++){
        System.out.println("arg["+i+"]: "+args[i]);
    }

}

}

I wanted to check the length of my command-line arguments and loop through them to display the array of command lines. 我想检查命令行参数的长度,并循环遍历以显示命令行数组。 However, it says my command line arguments are 0? 但是,它说我的命令行参数为0? How can this be? 怎么会这样?

Official Java tutorial about command-line arguments. 有关命令行参数的官方Java 教程

Command-Line Arguments 命令行参数

A Java application can accept any number of arguments from the command line. Java应用程序可以从命令行接受任意数量的参数。 This allows the user to specify configuration information when the application is launched. 这允许用户在启动应用程序时指定配置信息。

The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run. 用户在调用应用程序时输入命令行参数,并在要运行的类的名称后指定它们。 For example, suppose a Java application called Sort sorts lines in a file. 例如,假设一个名为Sort的Java应用程序对文件中的行进行排序。 To sort the data in a file named friends.txt, a user would enter: 要对名为friends.txt的文件中的数据进行排序,用户应输入:

java Sort friends.txt Java Sort friends.txt

When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of Strings. 启动应用程序时,运行时系统会通过字符串数组将命令行参数传递给应用程序的main方法。 In the previous example, the command-line arguments passed to the Sort application in an array that contains a single String: "friends.txt". 在上一个示例中,命令行参数在包含单个String的数组中传递给Sort应用程序:“ friends.txt”。

Echoing Command-Line Arguments 回显命令行参数

The Echo example displays each of its command-line arguments on a line by itself: Echo示例将其每个命令行参数单独显示在一行上:

 public class Echo { public static void main (String[] args) { for (String s: args) { System.out.println(s); } } } 

The following example shows how a user might run Echo. 以下示例显示用户如何运行Echo。 User input is in italics. 用户输入以斜体显示。

java Echo Drink Hot Java Java回声 喝热Java

Drink

Hot

Java Java的

Note that the application displays each word — Drink, Hot, and Java — on a line by itself. 请注意,该应用程序单独在一行上显示每个单词-Drink,Hot和Java。 This is because the space character separates command-line arguments. 这是因为空格字符分隔了命令行参数。 To have Drink, Hot, and Java interpreted as a single argument, the user would join them by enclosing them within quotation marks. 为了将Drink,Hot和Java解释为单个参数,用户可以通过将其括在引号中来加入它们。

java Echo "Drink Hot Java" Drink Hot Java java回声 “喝热的Java”喝热的Java

If you are using IDE (Eclipse or etc.) you have to specify command-line arguments via some kind of run configuration. 如果您使用的是IDE(Eclipse等),则必须通过某种运行配置来指定命令行参数。 For example for Eclipse : Eclipse为例:

日食

In have the command line is essentially of the form 在命令行中基本上是以下形式的

java [vm options] class/jar [arguments] java [vm选项]类/ jar [参数]

Only these final arguments are given to you in the array. 数组中仅将这些最终参数提供给您。 This is unlike a standard C program where you receive the command name also. 这与标准C程序不同,在标准C程序中,您也会收到命令名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM