Tuesday, 4 August 2015

Java Tutorial : Command line Argument

Want to level up your Java skills?

Subscribe to Ram N Java for simple, beginner-friendly programming lessons and clear code breakdowns.

Subscribe Now

What Are Command Line Arguments in Java?

A command-line argument is information passed directly to a Java program when you run it from the console or terminal. Instead of hardcoding values inside your code or prompting the user while the program is running, you provide the inputs immediately alongside the launch command.

How Do Command Line Arguments Work?

Whenever you execute a Java program, the Java Virtual Machine (JVM) gathers any words or values you typed after the class name, packages them into an array of strings, and delivers them directly to the args parameter of your main method:

public static void main(String[] args)

Key points to remember about these arguments:

  • Always Strings: Every argument received through args is stored as a String, even if you pass numeric digits like 10 or 25.
  • Zero-Indexed: The first argument is accessed at args[0], the second at args[1], and so on.
  • Separated by Spaces: Arguments are separated by whitespace when typed in the console.

Complete Example: Printing Supplied Arguments

Here is a simple example showing how to inspect and display the arguments passed into your application:

public class CommandLineDemo {
    public static void main(String[] args) {
        System.out.println("Total arguments: " + args.length);

        for (int i = 0; i < args.length; i++) {
            System.out.println("Argument at index " + i + ": " + args[i]);
        }
    }
}

Compiling and Passing Arguments in Terminal

Compile the file and supply your custom arguments directly after the class name:

javac CommandLineDemo.java
java CommandLineDemo Apple Banana Orange

Your terminal will print the following output:

Total arguments: 3
Argument at index 0: Apple
Argument at index 1: Banana
Argument at index 2: Orange

Recommended Java Tutorials From Our Channel

Explore these related tutorials from our channel to deepen your Java fundamentals:


Click here to watch in Youtube :
https://www.youtube.com/watch?v=pN8th2DKRjI&list=UUhwKlOVR041tngjerWxVccw

CommandLineArgExample.java
public class CommandLineArgExample
{
    public static void main(String[] args)
    {

        if (args != null && args.length > 0)
        {
            for (int i = 0; i < args.length; i++)
            {
                String commandLineArg = args[i];
                System.out.println("commandLineArg " + i + " : "
                        + commandLineArg);
            }
        }
        else
        {
            System.out.println("No command-line argument has been passed.");
        }

    }
}
Output
commandLineArg 0 : 100
commandLineArg 1 : 200
commandLineArg 2 : 300

Click the below Image to Enlarge
Java Tutorial : Command line Argument 
Java Tutorial : Command line Argument 
Java Tutorial : Command line Argument 
Java Tutorial : Command line Argument 
Java Tutorial : Command line Argument 
Java Tutorial : Command line Argument 
Java Tutorial : Command line Argument 
To Download CommandLineArgApp Project Click the below link
https://sites.google.com/site/javaee4321/java/CommandLineArgApp.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment

    Tutorials