- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Command line arguments in Java
Sometimes you will want to pass some information on a program when you run it. This is accomplished by passing command-line arguments to main( ).
A command-line argument is an information that directly follows the program's name on the command line when it is executed. To access the command-line arguments inside a Java program is quite easy. They are stored as strings in the String array passed to main( ).
Example
The following program displays all of the command-line arguments that it is called with -
public class CommandLine { public static void main(String args[]) { for(int i = 0; i<args.length; i++) { System.out.println("args[" + i + "]: " + args[i]); } } }
Try executing this program as shown here -
$java CommandLine this is a command line 200 -100
Output
This will produce the following result -
args[0]: this args[1]: is args[2]: a args[3]: command args[4]: line args[5]: 200 args[6]: -100
- Related Articles
- Java command line arguments
- Explain Java command line arguments.
- Command Line arguments in Java programming\n
- Command Line Arguments in Python
- Command Line arguments in C#
- Command Line arguments in Lua
- Command line arguments in C/C++
- Command line arguments example in C
- Command Line and Variable Arguments in Python?
- How to Parse Command Line Arguments in C++?
- How to add command line arguments in Python?
- getopt() function in C to parse command line arguments
- How do we access command line arguments in Python?
- How command line arguments are passed in main method in C#?
- How to pass command line arguments to a python Docker container?

Advertisements