HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Basics / Java Command Line Arguments

Java Command Line Arguments

The program arguments passed at the time of launching of the Java application are called command line arguments.

A Java program can be launched either from console or from an editor e.g. Eclipse. To launch a program we use "java className" command from command prompt or system console.

While launching the program, we can pass the additional arguments (no limit on the numbers of arguments) in below syntax.

1. Java Command Line Parameters Syntax

In given below syntax, we are passing 5 parameters to the Main class MyClass. MyClass has the main() method which accepts these arguments in form of an String array.

$ java MyClass arg1 arg2 arg3 arg4 arg5

2. Java Command Line Arguments Example

Let’s create an example to understand how command-line program arguments work in Java. This class simple accepts the arguments and prints them in the console.

As a programmer, we can use these arguments as startup parameters to customize the behavior of the application in runtime.

package app;

public class Main 
{
  public static void main(String[] args) 
  {
    for(int i = 0; i< args.length; i++) 
    {
      System.out.println( args[i] );
    }
  }
}

Now run this class from the console.

$ java Main 1 2 3 4

#prints

1
2
3
4

3. Summary

  • Command line args can be used to specify configuration information while launching the application.
  • There is no restriction on the maximum number of arguments. We can specify any number of arguments.
  • Arguments are passed as strings.
  • Passed arguments are retrieved as the string array in the main() method argument.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Basics

  • Java Introduction
  • Java Installation
  • Java Hello World
  • Java JDK, JRE and JVM
  • Java Classes and Objects
  • Java ClassPath
  • Java Operators
  • Java Data Types
  • Java Primitive Types
  • Java Variables
  • Java Comments
  • Java main() Method
  • Java Command Line Args
  • Java Wrapper Classes
  • Java Types of Statements
  • Java Block Statement
  • Java Pass-by-Value
  • Java System Properties
  • Java Static Import
  • Java hashCode() and equals()
  • Java Immutable Class
  • Java 32-bit vs 64-bit
  • Java java.exe vs javaw.exe
  • Java Generate Bytecode
  • Java Naming Conventions
  • Java Little-Endian vs Big-Endian

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces