Command line arguments in java

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!
Learn via video course
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
By Tarun Luthra
Free
5
Enrolled: 1000
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
Tarun Luthra
Free
5
Enrolled: 1000
Start Learning

Overview

Command line inputs are a way to provide input to Java applications through the command line when compiling and running the program. These are the inputs provided after the execution command when running a Java program. They are passed to the main function as strings and stored in the args parameter.

Command line inputs are processed by the Java Virtual Machine (JVM) before the main function is invoked. They are bundled together and supplied to the main function as an array of strings.

Command line inputs are read as strings, and certain characters like #, %, and & may cause errors when entered as command line arguments.

Scope

  • Introduction to Command Line Inputs: It explains how command line inputs can be used to provide input to Java applications.
  • Processing and Accessing Command Line Arguments: The article explores the process of passing command line arguments to Java programs and demonstrates how to access these arguments within the program.
  • Working with Command Line Arguments: The article discusses various techniques for working with command line arguments in Java.

Introduction

Every program deals with some type of data. This data can be either provided to it by the user or the programmer. The user hardly provides a big input to process. Whereas the programmer needs to provide various different types of data inputs for efficient testing. Now, this process may end up eating a lot of time on either manually entering inputs or hard coding data variables. We need to come up with a solution where we can do efficient testing with minimum effort and time.

There are various ways to provide input to our program. One of these ways is to provide input through the command line through which we also compile and run our program. These inputs are passed to the main function at runtime even before our main() begins to execute. The image below represents the two ways by which we can write our main function in Java. The argument String[] args or String… args can be broken down as follows.

In the context of command line arguments in Java:

  • String: It represents the data type of the elements in the array that is passed to the main function. In Java, command line arguments are received as an array of strings. The String data type is used because Java commonly uses strings for input and output operations. Each command line argument is treated as a string, regardless of its original data type.
  • args: It is the local name given to the array variable that holds the command line arguments in the main function. It serves as a reference to the array, allowing access to and modification of the values stored in it. By using the args variable, the program can process and manipulate the command line arguments as needed during runtime.

The command line inputs that are given to the program are stored in these main() function parameters.

Java programs are compiled and run by:

Compile > javac filename.java

Run > java filename

How are Command Line Inputs Processed?

In order to execute our program, we need to compile the .java file using the above-stated command. This converts our .java file to a .class file, which is in a machine-readable language. Later, we execute this file using the run command.

If we provide inputs following this command, the JVM (Java Virtual Machine) wraps all these inputs together and supplies them to the main function parameter String[] args or String... args. This process takes place even before our main() is invoked. These inputs are referred to as command line arguments. Since these inputs are passed to our application before its execution, they provide us with a way to configure our application with different parameters. This capability is particularly useful for efficient testing of our application.

How to Pass Command Line Arguments In Java?

To pass command line arguments in Java, follow these steps:

  1. Write your Java program and save it with the .java extension in your local system.
  2. Open the command prompt (cmd) and navigate to the directory where you saved your program.
  3. Compile your program using the command javac filename.java. This will convert the .java file to a .class file.
  4. Write the run command java filename and continue the command with all the inputs you want to give to your code.
  5. After writing all your inputs, run the command to execute your program.

How to Access Java Command Line Arguments?

The command line inputs given to the program are bundled and supplied to the main function parameter even before the invoking of the main function. These inputs are stored in a string array since Java deals with strings whenever it performs I/O operations.

The inputs are distinguished from one another by the blank space ' ' between them. The command line reads one character at a time and stores it. When it encounters a space, it saves the already collected text as an input value and repeats the process until all characters are read.

Code:

Output: Here after our execution command the following text is given:

The figure below gives a step by step walkthrough of how the command line inputs are read.

As soon as the run command gets executed, the inputs following it are read as strings into the main function parameter.

This is done in such a way that the JVM reads every string individually when it encounters a character. As it reads a space character it saves the string and starts with another string. This restricts the user to enter sentences as a command line input.

Command line arguments in java

How to Modify Command Line Arguments in Java?

The command line inputs are read as strings by the compiler and stored in an array. We can modify them by using the reference operator '[]'. The following program shows an example where we read the command line arguments and later modify them.

Here the input is accessed using the ‘[]’ operator and then we assign a new value to it. This assigned value overwrites the previous input.

Code:

Output:

Important Points about Command Line Inputs in Java

  • Command line inputs need to be provided after the Java filename in the command that executes our code.
  • Almost all data types are read as strings, but characters like '#', '%', and '&' throw an error when entered through the command line.
  • Command line inputs provide users with an alternative way to input data into our programs.
  • They are easy to access and can be manipulated just like normal arrays.
  • For handling large inputs, there are third-party libraries like Picocli and Spring Shell available.
  • These libraries are smaller in size, memory efficient, and faster compared to other libraries. They help in creating Java command line applications.
  • A Java application can accept any number of arguments from the command line. There is no restriction on the number of inputs that can be passed through the command line to the main function parameter.
  • Command line inputs are passed as strings to the main function parameter. For example, if the command line arguments are "123 abc @@@", they will be read as strings "123", "abc", and "@@@".
  • Command line inputs can be very useful for testing and running applications.
  • Initialization: Since command line inputs are provided before execution, they allow us to initialize our application with data values of our choice. This saves us from having to perform the same work within the code after it starts executing. For example, when testing a program with a large dataset, entering the inputs one by one would be impractical. By using command line inputs, we can easily and quickly provide a huge input to our program using the execution command.
  • Application Testing: Command line inputs enable us to provide different input values for testing our programs. This saves us from manually providing test values repeatedly. We can run multiple test values and create different real-life situations to test and debug our code. For example, instead of providing each test input separately, we can supply complete test data through command line inputs and test the program more efficiently.

Conclusion

  • Command line inputs in Java provide an alternative way for users to input data into our programs.
  • They are easy to access and manipulate, similar to normal arrays. Command line inputs can be passed as strings to the main() function parameter and are useful for testing and running applications.
  • These inputs, which are provided before execution, allow us to initialize our application with data values of our choice, providing a method for easy initialization.
  • Certain characters like '#', '%', and '&' may cause errors when entered through the command line.
  • Command line inputs are passed as strings to the main function parameter: args.
  • The command line reads characters one by one, and input values are separated by spaces. Each time a space is encountered, the collected text is saved as an input value.