Home  >  Article  >  Java  >  How to use command line arguments in Java

How to use command line arguments in Java

藏色散人
藏色散人Original
2019-01-19 10:46:1517482browse

How to use command line parameters in Java: First run the Java application from the terminal window; then pass the parameters to the starting point of the application.

How to use command line arguments in Java

The operating environment of this article: Windows 7 system, Dell G3 computer, Java version 8.0.

Command line parameters can be a way to specify configuration properties for an application, and Java is no exception. You can run Java applications from a terminal window instead of clicking the application icon in the operating system. In addition to the application name, it can be followed by a number of parameters, which are then passed to the starting point of the application (i.e. the main method in the case of Java).

For example, NetBeans has a number of startup parameters that can be passed to the application when running from a terminal window (for example, -jdkhome specifies the version of the JDK to use other than the one associated with the NetBeans application Default JDK).

main method

Let’s check the main method to see where the parameters passed to the application appear:

public static void main(String[] args)
{
...do something here
}

Command line parameters are OK Find args in the String array named.

For example, let us consider an application called CommandLineArgs whose only action is to print the command line arguments passed to it:

public class CommandLineArgs {
    public static void main(String[] args) {
        //检查字符串数组是否为空
        if (args.length == 0)
        {
            System.out.println("There were no commandline arguments passed!");
        }
        //对于字符串数组中的每个字符串
        //打印出字符串。
        for(String argument: args)
        {
            System.out.println(argument);
        }
    }
}

Syntax for command line arguments

The Java Runtime Engine (JRE) expects parameters to be passed following a specific syntax, as shown below:

java ProgramName value1 value2

In the above, the JRE is called with "java" followed by the name of the program you are calling. Next are any parameters for the program. There is no limit to the number of arguments a program can take, but the order is important. The JRE passes arguments in the order they appear on the command line. For example, consider the above code snippet:

public class CommandLineArgs2 {

    public static void main(String[] args) {
        if (args.length == 0)
        {
            System.out.println("There were no commandline arguments passed!");
        }

When arguments are passed to a Java program, args[0] is the first element of the array (value1 above) and args[1] is the second element ( value2), and so on. The length() code defines the length of the array.

Passing Command Line Parameters

In NetBeans we can pass command line parameters without having to build the application and run it from a terminal window. To specify command line parameters:

Right-click the Projects folder in the Projects window.

Select the Properties option to open the project properties window.

In the Categories list on the right, select Run.

In the Arguments text box that appears, specify the command line parameters to be passed to the application. For example, if we enter Apple Banana Carrot in the Arguments text box and run the CommandLineArgs program listed above, we will get the output:

Apple
Banana
Carrot

Parsing command line arguments

Typically, a command line argument is passed that contains some information about what to do with the passed value. Parameters that notify the application of parameters usually have a hyphen or two before their name. For example, the NetBeans example for specifying a startup parameter for the JDK path is -jdkhome.

This means you need to parse the command line arguments to determine what to do with the values. There are several Java command line frameworks for parsing command line arguments. Alternatively, if you don't have many arguments to pass, you can write a simple command line parser:

public class CommandLineArgs {
//命令行参数:
// -打印输出输出它后面的所有参数
//addnumbers在后面添加所有的数字参数
public static void main(String[] args) {
//检查字符串数组是否为空
if (args.length == 0)
{
System.out.println("There were no commandline arguments passed!");
}
else
{
  // 设置一些初始变量
boolean printout = false;
boolean addNumbers = false;
boolean validNumbers = true;
int total = 0;
for(String argument: args)
{
if(argument.equals("-addnumbers"))
{
printout = false;
addNumbers = true;
}
else if (argument.equals("-printout"))
{
printout = true;
addNumbers = false;
}
else if (addNumbers)
{
try {
total = total + Integer.parseInt(argument);
} catch (NumberFormatException e) {
System.out.println("arguments passed with -addnumbers " + "must be integers!");
validNumbers = false;
addNumbers = false;
}
}
else if (printout)
{
System.out.println(argument);
}
}
if (validNumbers)
{
System.out.println("The total of the number arguments is: " + total);
}
}
}
}

The code above will either print the arguments or add them (if they are integers). For example, this command line argument will add numbers:

java CommandLineArgs -addnumbers 11 22 33 44

The above is the detailed content of How to use command line arguments in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more