How to use command line arguments in Java
Jan 19, 2019 am 10:46 AMHow 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.
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 |
|
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 |
|
Syntax for command line arguments
The Java Runtime Engine (JRE) expects parameters to be passed following a specific syntax, as shown below:
1 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed

Node.js 20: Key Performance Boosts and New Features

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 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?
