What is the Java Main Method and how to create one ?

Java Conceptuel Diagram

About

This page is about the main method in Java (ie the startup point of every app).

Example

This is a quick example that shows how to write a main method.

IDE may create it for you via template if you write the short notation psvm followed by a tab

package com.datacadamia.myapp;

public class MyMain {

        // the main method that is called from the command line with the arguments args
	public static void main(String[] args) throws Exception {
                
                // The local variables
		String myFirstParameter = null;
		boolean printTrue = false;
                
                // Treatment of the args parameters
		for (int i = 0; i < args.length; i++) {
			if (args[i].startsWith("-printTrue")) {
				printTrue = true;
			} else if (args[i].equals("-usage")) {
				usage();
			} else if (args[i].equals("-help")) {
				usage();
			} else {
				myFirstParameter = args[i];

				// Must be last arg
				if (i != args.length - 1) {
					usage();
				}
			}
		}

                // Last usage verification
		if (myFirstParameter == null) {
			usage();
		}
                
                
                // The actions to performed by the application
		System.err.println("FirstParameter: " + myFirstParameter);
		if (printTrue) {
			System.err.println("True");
		}
	}

	private static void usage() {
		System.err.println("Usage: MyMain [-options] <myFirstParameter>");
		System.err.println("       -printTrue = print the word true");
		System.err.println("       -usage or -help = this message");
		System.exit(1);
	}

}

How to write a main method?

In the Java programming language, every application must contain a main method whose signature is:

public static void main(String[] args)

The main method accepts a single parameter, usually named args, whose type is an array of String objects.

The public static void keywords mean:

  • the Java virtual machine (JVM) interpreter can call the method to start the program (public) without creating an instance of the class (static),
  • the program does not return data to the Java VM interpreter (void) when it ends.

The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above.

The argument

You can name the argument anything you want, but most programmers choose “args” or “argv”.

The main method accepts a single argument: an array of elements of type String.

This array is the mechanism through which the runtime system passes information to your application. For example:

java -jar MyApp.jar arg1 arg2

Each string in the array is called a command-line argument.

For example, a sorting program might allow the user to specify that the data be sorted in descending order with this command-line argument:

java -jar MyApp.jar -descending

Library

To parse the command arguments, you can use the Apache Commons CLI library





Discover More
Gradle - Application Plugin

The Application plugin allows you to: get all applications JARs as well as all of their transitive dependencies add two startup scripts (one for UNIX-like operations systems and one for Windows)...
Java Conceptuel Diagram
Java - (Class|Object|Member) (Initialization|Instantiation)

Class (Initialization|Instantiation) in java. During an class initialization: Java's class loader loads the main method Java's byte code verifier verifies the class. The first initialization...
Eclipse Main Class
Java - (Main|Start) Class

in Java is called a Main class. The (Main|Start) class is a class that: have a main method will start the main thread (process) main classjar You can start the java application by: calling...
Java Conceptuel Diagram
Java - (Method|Functions)

A function that belong to an object is called a methods. A static method is then a sort of function. Methods: operate on an object's internal state and serve as the primary mechanism for object-to-object...
Java Conceptuel Diagram
Java - Applet

An applet is a small client application written in the Java programming language that executes in the Java virtual machine installed in the Web browser. However, client systems will likely need the Java...
Java Conceptuel Diagram
Java - Application

An application in shipped in an archive format with a main class that contains a main method that starts the application. instancejava/lang/Runtimeclass RuntimeOperating System environment ...
Simple Class
Java - Class (Definition)

A java/lang/Classclass provides the blueprint for objects; you create an object from a class. All classes are derived from the Object class. A class declaration names the class and encloses the class...
Java Control Panel
Java - Getting Started - Hello World

A little article on the basics of Java. On Windows, the Java Control Panel: Creation of the HelloWorld.java file as text file. This simple code is composed of: a class HelloWorld a main...
Java Conceptuel Diagram
Java - Java Virtual Machine (JVM|Java)

Ie the java executable: Different JVM exist and are embedded/available in a image Options that: begin with -X are non-standard (not guaranteed to be supported on all VM implementations), and are...
Eclipse Oepe Junit Library
Java - Junit - How do I write and run a simple test?

Steps on how to write a sample junit test. Write a SimpleTest.java file To compile your JUnit tests, you'll need the following elements in your CLASSPATH: the...



Share this page:
Follow us:
Task Runner