# \[Tutorial\] Command Line Arguments In Java With Example!

**URL:** <https://onehack.st/t/tutorial-command-line-arguments-in-java-with-example/58422>\
**Category:** Tutorials & Methods\
**Tags:** tools, tips-tricks\
**Created:** [September 21, 2019, 11:08am UTC](https://onehack.st/t/tutorial-command-line-arguments-in-java-with-example/58422 "2019-09-21T11:08:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aina](https://onehack.st/user_avatar/onehack.st/aina/32/167469_2.png) [@Aina](https://onehack.st/u/Aina)\
**Post date:** [September 21, 2019, 11:08am UTC](https://onehack.st/t/tutorial-command-line-arguments-in-java-with-example/58422/1 "2019-09-21T11:08:34Z")

</div>

## What is the Command Line Argument?

Command Line Argument is information passed to the program when you run the program. The passed information is stored as a string array in the main method. Later, you can use the command line arguments in your program.

**Example** While running a class **Demo** , you can specify command-line arguments as

```auto
java Demo arg1 arg2 arg3 …
```

## Command Line Arguments in Java: Important Points

- Command Line Arguments can be used to specify configuration information while launching your application.
- There is no restriction on the number of java command line arguments. You can specify any number of arguments
- Information is passed as Strings.
- They are captured into the String args of your main method

**Example: To Learn java Command Line Arguments**

**Step 1)** Copy the following code into an editor.

```auto
class Demo{
     public static void main(String b[]){
         System.out.println("Argument one = "+b[0]);
         System.out.println("Argument two = "+b[1]);
    }
}
```

**Step 2)** Save & Compile the code

**Step 3)** Run the code as **java Demo apple orange.**

![1](https://onehack.st/uploads/default/original/3X/5/8/584a34fa24d41f17f30b4dafb58d4f0441a16602.jpeg)

**Step 4)** You must get an output as below.

![2](https://onehack.st/uploads/default/original/3X/d/f/df73b22d430984e98912e66900f1bcd5d3ba0070.jpeg)

## ENJOY & HAPPY LEARNING! 👍

---

<div class="post-metadata">

**Author:** ![goku\_pistol](https://onehack.st/user_avatar/onehack.st/goku_pistol/32/24327_2.png) [@goku\_pistol](https://onehack.st/u/goku_pistol)\
**Post date:** [September 21, 2019, 11:24am UTC](https://onehack.st/t/tutorial-command-line-arguments-in-java-with-example/58422/2 "2019-09-21T11:24:10Z")

</div>

I always used to mess up between args[0],args[1] (here b[0],b[1]),  
as in c/c++ & python arg[0] was program name followed by arguments 😅
