Home Java javaTutorial How to use command line arguments in Java

How to use command line arguments in Java

Jan 19, 2019 am 10:46 AM

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:

1

2

3

4

public static void main(String[] args)

{

...do something here

}

Copy after login

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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);

        }

    }

}

Copy after login

Syntax for command line arguments

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

1

java ProgramName value1 value2

Copy after login

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:

1

2

3

4

5

6

7

public class CommandLineArgs2 {

 

    public static void main(String[] args) {

        if (args.length == 0)

        {

            System.out.println("There were no commandline arguments passed!");

        }

Copy after login

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:

1

2

3

Apple

Banana

Carrot

Copy after login

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

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);

}

}

}

}

Copy after login

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

1

java CommandLineArgs -addnumbers 11 22 33 44

Copy after login

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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Mar 07, 2025 pm 06:09 PM

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

How does Java's classloading mechanism work, including different classloaders and their delegation models?

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Mar 07, 2025 pm 05:52 PM

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed

Node.js 20: Key Performance Boosts and New Features Node.js 20: Key Performance Boosts and New Features Mar 07, 2025 pm 06:12 PM

Node.js 20: Key Performance Boosts and New Features

Iceberg: The Future of Data Lake Tables Iceberg: The Future of Data Lake Tables Mar 07, 2025 pm 06:31 PM

Iceberg: The Future of Data Lake Tables

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?

See all articles