DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > How to Modify Java Command-Line Arguments

How to Modify Java Command-Line Arguments

Learn how to modify Java command-line arguments. Java command-line arguments enable programmers to pass the arguments during the execution of the program.

Bikash Daga user avatar by
Bikash Daga
·
Jun. 09, 22 · Java Zone · Tutorial
Like (5)
Save
Tweet
2.31K Views

Join the DZone community and get the full member experience.

Join For Free

How do you check that your code is error-free, works in all situations, and handles all the edge cases?

Well, the simpler way is to check it against all the possible ranges of test cases, which can be really vast sometimes. Passing these test cases could be really hectic and time-consuming in such scenarios. We need some way that can get this done easily without much hassle and can be automated for bigger inputs. Command-line arguments in Java are one such solution to our problem to make our testing and debugging process easier.

In this article, we will discuss briefly what command-line arguments are, how they are passed, and how they work; but our main focus will be on how to modify Java command-line arguments.

Java
 
// here args will hold the command line arguments
    public static void main(String[] args) {
        System.out.println("Hello World!");
        // remaining code will go here
    }


What Are Command-Line Arguments?

Java command-line arguments enable programmers to pass the arguments during the execution of the program. The programmers can pass these arguments directly from the console which will be accessed by the main() method and can be used as inputs. We can also bypass the command-line arguments by passing values directly to the main() method.

How Do Command-Line Arguments Works?

Command-line arguments that are provided while executing are wrapped up and supplied to args[]. Args[] is essentially an array of string that holds all the command-line arguments provided to it. Each argument will be stored at an index, starting from args[0] all the way to args[n]. 

How Do You Pass Command-Line Arguments?

Command-line arguments are passed through command prompts or terminals. All the arguments passed through it will be converted and wrapped into args[] array internally by JVM. 

Now let’s see how to pass command-line arguments with the help of an example. Suppose we have a file with some Java code. The following steps will demonstrate how to pass command-line arguments in Java:

  1. Save the Java program with the .java extension (e.g. Arguments.java).

  2. Open terminal/command prompt into the directory where the program is stored locally.
  3. Compile the Java program to convert .java file to .class file

    • Command: javac filename.java (Here, javac Arguments.java)

  4. Run the program and pass the arguments after the filename is separated by spaces.
    • Command: java filename argument1 argument2 …. argumentN (Here, Java arguments Welcome to this blog and Happy Learning)

How Do You Access Command-Line Arguments?

As discussed in the case above, let’s try to access all those passed command-line arguments in Java now. How do we do it?

Since these arguments are stored in the args[] array, that means we can access them easily using args[i], where I specify an index that can vary from 0 to n (total numbers of arguments passed).

Below code is the demonstration of the same:

Java
 
class Arguments {
    public static void main( String[] args ) {
        System.out.println(" Hello World! ");
        // args.length is used to get length of args array

        System.out.println(" Total number of args: " + args.length);
        // iterating over the args array to print the args if available 

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


Output

Java
 
 Hello World! 
 Total number of args: 7
 Arg 0: Welcome
 Arg 1: to
 Arg 2: this
 Arg 3: blog
 Arg 4: and
 Arg 5: Happy
 Arg 6: Learning


How Do You Modify Command-Line Arguments?

Again, we can make use of args[i] to get value at ith index, and then use assignment operator (=), we can modify the command-line argument at ith index.

The below snippet will modify some of the arguments passed through the command line in Java.

Java
 
args[1] = " Modified Command Line Argument 1 ";
args[3] = " Modified Command Line Argument 2 ";
args[5] = " Modified Command Line Argument 3 ";


Updated Output

Java
 
 Hello World! 
 Total number of args: 7
 Arg 0: Welcome
 Arg 1:  Modified Command Line Argument 1 
 Arg 2: this
 Arg 3:  Modified Command Line Argument 2 
 Arg 4: and
 Arg 5:  Modified Command Line Argument 3 
 Arg 6: Learning


In a Nutshell

  • Java command-line arguments are a way of providing inputs to the program through a command prompt.

  • Command-line arguments are passed while executing the run command, arguments are passed immediately after the filename, separated by spaces.

  • Command-line arguments are eventually received by an array of strings (args[]) in the main function.

  • We can access or modify the command-line arguments using args[i] and assignment operators just like we do in normal programming languages.

Command (computing) Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Internal Developer Platform in Plain English
  • Making Your Own Express Middleware
  • Java Microservices: Code Examples, Tutorials, and More
  • What Is Selenium? Getting Started With Selenium Automation Testing

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo