3 minute read

A Java application consists of one or more classes. To enable an execution, a Java application must have exactly one class with the main method that has been designated as the main class, where the execution will start from.

The default main method makes a CLI application. CLI stands for command line interface. It takes any number of arguments from the command line.

To make a GUI-based, graphical user interface, application, the interface should be carefully designed for streamlining operations. This is related to user experience (UX) and it requires an understanding of aesthetics.

The Default main Method

By convention, the main method often takes the following format for its method signature:

public static void main(String[] args) {
	//statements here..
}

args

In the main method, the parameter args is a String array which will receive input from the user to launch the application in a console.

The following shows a few common uses of args, very similar to

  • args.length returns the total number of arguments that the user have entered.
  • args[0] returns the first argument, args[1] returns the second one and so on.

To iterate args, one way is shown below by using enhanced for loop:

public static void main(String[] args) {
   for(String a: args){
       System.out.println(a);
   }
}

args - Example

To test the main method above, if you are using an IDE such as NetBeans, make a new project named Echo. In the project, make a Java class Echo and add the main method. See the source below.

public class Echo {
    public static void main(String[] args) {
        for (String a : args) {
            System.out.println(a);
        }
    }
}

Echo is executable as it contains the main method. There are mainly three ways of running an application.

Launch the Application (Inside an IDE)

Set Run Configuration:

Inside the NetBeans IDE (8.1), right click the project name Echo. Select Set Configuration. Choose Customize.... In the following dialog box, pick a Main Class. Enter your input in the field Arguments. For example, five arguments, “coffee data 12 anything better”, are separated by space.

After setting the configuration, the application is ready for launch.

Launch the application: Again right click the project name Echo. Choose Run. The output will echo all the arguments, each in a line.

Launch the Application (w/o the IDE)

In order to call a Java application from the command line, a distribution needs to be prepared in a JAR file.

How do we create a JAR file? Read this post [Packaging Java Application in a JAR]( {{site.url}}{{site.baseurl}}{% post_url 2017-01-17-java-archive-file %} ).

After a jar file has been prepared, the following command will launch Echo.

java -jar "Echo.jar" coffee data 12 anything better

Exercise

Use the default main method as a reference. Design the main method for the Triangle class. Triangle.java is available in

A Quick Guide to Classes, Instances, Properties and Methods in Java.

The main method should take three arguments from the user, create a new Triangle instance and assign the three arguments as integers to three side lengths a, b and c in order. Then the user should be notified by the text in the console, telling the user if his triangle is valid or not.

Run the revised class inside the IDE (NetBeans).

Note: Each command-line argument in the main method is of type String. In order to take the numerical value from an argument, apply Integer.parseInt to the argument.

For example, to convert (parse) the second argument in args to a numerical value, the expression is

Integer.parseInt(args[1])
comments powered by Disqus