close
The Wayback Machine - https://web.archive.org/web/20240805050453/https://www.geeksforgeeks.org/command-line-arguments-in-java/
Open In App

Command Line Arguments in Java

Last Updated : 20 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Java command-line argument is an argument i.e. passed at the time of running the Java program. In Java, the command line arguments passed from the console can be received in the Java program and they can be used as input. The users can pass the arguments during the execution bypassing the command-line arguments inside the main() method.

Working command-line arguments

We need to pass the arguments as space-separated values. We can pass both strings and primitive data types(int, double, float, char, etc) as command-line arguments. These arguments convert into a string array and are provided to the main() function as a string array argument.

When command-line arguments are supplied to JVM, JVM wraps these and supplies them to args[]. It can be confirmed that they are wrapped up in an args array by checking the length of args using args.length.

Internally, JVM wraps up these command-line arguments into the args[ ] array that we pass into the main() function. We can check these arguments using args.length method. JVM stores the first command-line argument at args[0], the second at args[1], the third at args[2], and so on.

Examples of command-line argument

Example 1:

Java




// Java Program to Illustrate First Argument
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Printing the first argument
        System.out.println(args[0]);
    }
}


Output

Output of Java Command line Arguments

 

Explanation of the above code:

If we run a Java Program by writing the command “java Hello Geeks At GeeksForGeeks” where the name of the class is “Hello”, then it will run up to Hello. It is a command up to “Hello” and after that i.e “Geeks At GeeksForGeeks”, these are command-line arguments.

Example 2

Java




// Java Program to Check for Command Line Arguments
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Checking if length of args array is
        // greater than 0
        if (args.length > 0) {
 
            // Print statements
            System.out.println("The command line"
                               + " arguments are:");
 
            // Iterating the args array
            // using for each loop
 
            for (String val : args)
                // Printing command line arguments
                System.out.println(val);
        }
        else
            // Print statements
            System.out.println("No command line "
                               + "arguments found.");
    }
}


Output

Output of the command line argument in Java

The steps to Run the Above Program are:

 To compile and run a Java program in the command prompt, follow the steps written below.

  • Save the program as Hello.java
  • Open the command prompt window and compile the program- javac Hello.java
  • After a successful compilation of the program, run the following command by writing the arguments- java Hello
  • For example – java Hello Geeks at GeeksforGeeks
  • Press Enter and you will get the desired output.

 



Similar Reads

getopt() function in C to parse command line arguments
The getopt() function is a builtin function in C and is used to parse command line arguments. Syntax: getopt(int argc, char *const argv[], const char *optstring) optstring is simply a list of characters, each representing a single character option. Return Value: The getopt() function returns different values: If the option takes a value, that value
2 min read
Scan conversion of Line and Line Drawing algorithms
SCAN Conversion of Line : A line connects two points.It is a basic element in graphics.You'll need two spots between which to draw a line to draw a line . A line, or line segment, can be uniquely described by two points, according to geometry. We also know from algebra that a line can be defined by a slope, commonly denoted by the letter m, and a y
4 min read
Level order traversal line by line | Set 3 (Using One Queue)
Given a Binary Tree, print the nodes level-wise, each level on a new line. Output: 1 2 3 4 5Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. We have discussed two solutions in the articles below. Print level order traversal line by line | Set 1 Level order traversal line by line | Set 2 (Using Two Queues)In this p
6 min read
Arguments in Discrete Mathematics
Arguments are an important part of logical reasoning and philosophy. It also plays a vital role in mathematical proofs. In this article, we will throw some light on arguments in logical reasoning. Logical proofs can be proven by mathematical logic. The proof is a valid argument that determines the truth values of mathematical statements. The argume
4 min read
uniq command in Linux with examples
The linux uniq command is basically used to remove all repeated lines in a file. This command is used when a line is repeated multiple times and replace these multiple lines with one line. This command is designed to work on sorted files. Purpose : Remove repetitious lines from the sorted 'input-file' and send unique lines to the 'output-file'. If
2 min read
pr command in Linux
In Linux/Unix pr command is used to prepare a file for printing by adding suitable footers, headers, and the formatted text. pr command actually adds 5 lines of margin both at the top and bottom of the page. The header part shows the date and time of the last modification of the file with the file name and the page number. Syntax: pr [options][file
2 min read
cmd | cls command
Cls is a internal command found inside windows command interpreter cmd, that is used for clearing the console window. It does so by removing the entry of all previously entered commands, and their corresponding outputs. The command was first introduced in the MSDOS Version 2. Initially it was for command.com (command processor for MSDOS), which is
2 min read
How to List Current Logged-In Users in Linux | who Command
Linux, being a powerful and versatile operating system, provides several command-line tools to manage and monitor user activity. One common administrative task is to identify and list the currently logged-in users on a Linux system. In this article, we will delve into multiple ways to achieve this, exploring commands such as who, user, w, last, fin
5 min read
'IPCS' command in Linux with examples
ipcs shows information on the inter-process communication facilities for which the calling process has read access. By default, it shows information about all three resources: shared memory segments, message queues, and semaphore arrays. Without options, the information shall be written in short format for message queues, shared memory segments, an
3 min read
One line function for factorial of a number
Factorial of a non-negative integer, is multiplication of all integers smaller than or equal to n. Example : Factorial of 6 is 6 * 5 * 4 * 3 * 2 * 1 which is 720. We can find the factorial of a number in one line with the help of Ternary operator or commonly known as Conditional operator in recursion. C/C++ Code // C++ program to find factorial of
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg