(Updated September 10, 2021)
Overview
Using an IDE to build and run programs is typically only done during development. Many developed programs will run on servers without a text or GUI interface of any kind and will write errors and such to log files for review when something goes wrong.
Since these programs will often be executed from scripts that invoke the program through the JRE behind the scenes, there needs to be a mechanism to pass additional information to the program. This is where the command line interface comes into play.
Review – Arrays of Objects
In Chapter 9 there is a section on Arrays of Objects that touches on how to use args
to get access to the data passed to the program.
As an application developer, you will need a method to simulate this from your IDE. This is very easily done with IntelliJ IDEA.
From within IntelliJ, you will:
- Select Run from the top menu.
- Select Edit Configurations.
Now fill in the Program arguments box with whatever you need to pass to your program. An example is shown below using “43b:16 100101:2 247:8
“.
Testing
You can confirm that the values are being passed in with the following example main
method.
public static void main(String[] args)
{
int x;
for (x=0; x < args.length; x++)
System.out.println("args[" + x + "] = " + args[x]);
}
You will see the following output if everything is configured correctly.
args[0] = 43b:16 args[1] = 100101:2 args[2] = 247:8