This tutorial explains How to pass command line arguments to a Java application.

How to pass command line arguments to java program in Netbeans

To pass Command Line arguments to Java programs, Please follow the below steps.

  • Open the project or a file
  • Go to the Run Menu, Select Set Project Configuration, Select Customize Option
  • It opens the Project Properties Window
  • Select the Left side Run tab under the categories label The right side shows form and enters the Arguments input box with values one two three.
  • Now run the file using one of the options
    • Select Run + Run File option( use Shortcut Shift + F6)
    • Select Run + Run Main Project option ( use Shortcut F6)
  • Given arguments are passed to args of the main method in the java program as given below
package com.mycompany.myapp;
/**
 *
 * @author abc
 */
public class FirstProgram {

    public static void main(String[] args) {

        for (String s : args) {
            System.out.println(s);
        }

    }
}

It prints command line values.

one
two
three

How to pass command line arguments to maven projects in Netbeans

For Maven, you can pass properties that contain key and value pairs.

To pass command line arguments to maven projects in Netbeans, please follow the below steps.

  • Open the project or a file
  • Go to the Run Menu, Select Set Project Configuration, Select Customize Option
  • It opens the Project Properties Window
  • Select the Left side Actions tab under the Categories label
  • Right side, Select Run file via main() option from a dropdown in actions label
  • You can pass properties such as key1=value1
  • Save the settings by clicking OK
  • Now run the file using one of the options
    • Select Run + Run File option( use Shortcut Shift + F6)
    • Select Run + Run Main Project option ( use Shortcut F6)
  • Given arguments are passed to args of the main method in the maven project program as given below