Command-line arguments are a useful way to gather input for our console application.
In this post, I’ll show how to use these types of arguments in your console applications.
Table of Contents
- Why do we use command-line arguments?
- Capturing command-line arguments in a Java Console Application
- Execution of a console application with command-line arguments
- Using IntelliJ IDE
Why do we use command-line arguments?
As you should know by now, algorithms solve a problem by taking input and calculating an output.
For instance, you can have an algorithm that takes a number as input and returns if the number is odd or not as output.
Because in this case the algorithm uses only one number as input, and won’t ask the user for another number, this input can be a command-line argument.
However, if you want to provide a console menu for the user, then maybe you shouldn’t use command-line arguments.
Several console applications and services, however, use command-line arguments to define modes of execution. These modes of execution can also be stated using environment variables.
As you can see, the use of command-line arguments depends on the problem you are trying to solve. But one can say that you can use them anytime you need to pass a parameter to an application and, the value on that parameter will be used while the application is running.
Capturing command-line arguments in a Java Console Application
See below the code for an empty Java Console Application.
package com.RP; public class Main { public static void main(String[] args) { } }
The method main is the entry point for the execution of the console app. Notice that the method has a parameter named args. By using that variable, we can access the command-line arguments, if there is any.
Let’s say we are expecting two command-line arguments, the name of a person and its age (in that order). We can capture the two values as follows:
package com.RP; import static java.lang.Integer.parseInt; import static java.lang.System.exit; public class Main { public static void main(String[] args) { String name; int age; if (args.length==2){ name = args[0]; age = parseInt(args[1]); System.out.println("I read the arguments1"); } else{ System.out.println("Usage: java Main name age"); exit(1); } } }
Notice that the age of a person is an integer, but all the command-line arguments are stored as a string. Therefore, we must convert the second argument to an int.
Execution of a console application with command-line arguments
To execute the application above, you just must open a terminal on the same folder that your java file is.
You can check if you are in the right folder by using the ‘ls’ command if you are in a Unix based operating system, or the ‘dir’ command if you are using windows.
The name of the file in my case is ‘Main.Java’. You can have a different file name, but notice that if the class is declared as public, the name of the file must be the same as the name of the class.
Executing the ‘ls’ command we can verify that we are on the right folder. As you can see, there is a file named Main.java, which is our source code.
Now we must enter two commands:
- Javac Main.java
- Java Main
Now, because you didn’t pass the arguments, you should get a result like the one below.
You get this result because, in the code, we stated that if no arguments were passed, we will print a message that shows the user how to use our console application.
Using IntelliJ IDE
If you are using an IDE, like IntelliJ for instance, you should get a package name in your java file. You will see that the first line of your file looks like this one:
package com.RP;
In this case, to execute this file we can do it as follows:
First, click on the link ‘Terminal’ on the bottom left of the IDE window (see picture below).
Then, execute the command: “javac com/RP/Main.java”. com/RP will vary depending on the name of the package as it will show on the first line of your file. Notice that the package name on my example is “com.RP” and the class name is Main, that is why we have to execute the command using com/RP/Main.java .
Lastly, you can execute your console application using the following command:
“java com/RP/Main”
Notice that because you didn’t specify any argument, you will get an error message on how you should use this console application.
To pass the arguments, you can just follow the usage message when you don’t pass the arguments.
See below an example of execution from the console.
H@ppy coding!