Tutorials  /  JavaScript

How to Work with Command-Line Arguments in Java

Ccentron Redaktion · November 2024 ·3 min read ·JavaScript, Tutorial

Command-line arguments are an essential aspect of programming in Java, allowing parameters to be passed directly to the main program. These arguments are treated as strings and provide a dynamic way to interact with applications during runtime. Below, we’ll explore how command-line arguments work, how to use them, and how to test them efficiently in your development process.

What Are Command-Line Arguments?

In Java, the main method accepts a String array as its parameter:

Code
public static void main(String[] args)

This array, args, holds the values provided as command-line inputs. These inputs must be space-separated and are automatically converted into strings before being passed to the program. The flexibility of command-line arguments allows passing both textual and numerical data.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Example: Printing Command-Line Arguments

To illustrate the use of command-line arguments, let’s create a simple Java program that prints the number of arguments passed and their respective values:

Go
package com.example.commandline;

public class CommandLineExample {
    public static void main(String[] args) {
        System.out.println("Number of Command-Line Arguments: " + args.length);

        for (int i = 0; i < args.length; i++) {
            System.out.println(String.format("Argument %d: %s", i, args[i]));
        }
    }
}

Running the Program Without Arguments

When executed without arguments, the output will be as follows:

Console
$ java com/example/commandline/CommandLineExample
Number of Command-Line Arguments: 0

Running the Program With Arguments

Passing arguments as space-separated values provides the following results:

Console
$ java com/example/commandline/CommandLineExample "Hello" "World"
Number of Command-Line Arguments: 2
Argument 0: Hello
Argument 1: World

This also works for numerical values, which will still be treated as strings unless explicitly converted:

Console
$ java com/example/commandline/CommandLineExample 1 2 3
Number of Command-Line Arguments: 3
Argument 0: 1
Argument 1: 2
Argument 2: 3

Using Command-Line Arguments in Eclipse

Command-line arguments can also be tested within Eclipse by configuring the run settings.

  1. Open Run Configurations:

    Right-click the Java class file and select Run As -> Run Configurations.

  2. Set Arguments:

    Navigate to the Arguments tab. Enter the desired arguments in the “Program Arguments” text field.

  3. Execute the Program:

    Click Run to execute the program with the specified arguments.

Practical Applications

Command-line arguments are a versatile tool for passing runtime configurations. For instance:

  • Database Connections: Specify database credentials for dynamic program initialization.
  • Configuration Files: Provide file paths for reading configuration data.
  • Testing: Pass varying inputs to test application robustness under different conditions.

Conclusion

Mastering command-line arguments in Java is a key skill for developers, enabling efficient program configuration and execution. Whether you’re passing input directly via the terminal or through an IDE like Eclipse, understanding how to handle these arguments ensures better flexibility and functionality for your applications.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren