DZone
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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • JGit Library Examples in Java
  • Quickly Find Your Java Application Process ID
  • How to Kill Processes in Unix/Linux
  • Step By Step Guide To Using Mule ESB

Trending

  • Top Book Picks for Site Reliability Engineers
  • DGS GraphQL and Spring Boot
  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  • Agentic AI for Automated Application Security and Vulnerability Management
  1. DZone
  2. Coding
  3. Java
  4. 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.

By 
Bikash Jain user avatar
Bikash Jain
·
Updated Jun. 09, 22 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
4.6K 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.

Related

  • JGit Library Examples in Java
  • Quickly Find Your Java Application Process ID
  • How to Kill Processes in Unix/Linux
  • Step By Step Guide To Using Mule ESB

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!