Home Maven Maven - How to run Main class?

Maven – How to run Main class?

In this tutorial, we discuss how to run main method of Java class in your project with or without arguments using Maven command.

1. Run main class using Maven

Maven exec plugin provides an option exec:java to run a main class program of your java project. There are several options to use this plugin, let’s see few of them.

Let’s say we have 2 main classes MainClass1 and MainClass2 in our java project in com.javabydeveloper.util package.

MainClass1:

public class MainClass1 {

	public static void main(String[] args) {
		
		System.out.println("---- Main Class1 -----");

		if(args.length > 0)
			Arrays.asList(args).forEach(System.out::println);
		else
			System.out.println("No arguments recieved!");
	}
}

MainClass2:

public class MainClass2 {

	public static void main(String[] args) {
		
		System.out.println("---- Main Class2 -----");
		
		if(args.length > 0)
			Arrays.asList(args).forEach(System.out::println);
		else
			System.out.println("No arguments recieved!");
	}
}

1.1. Directly from command

You can use following command to run specific Java main program by providing fully qualified name to the exec.mainClass system property.

$ mvn exec:java -Dexec.mainClass="com.javabydeveloper.util.MainClass1"

Results:

---- Main Class1 -----
No arguments recieved!

1.2. Using properties in pom.xml

Suppose that you have several main classes in your Java project, by default you would need to run MainClass1, you can configure as Maven property.

	<properties>
		<exec.mainClass>com.javabydeveloper.util.MainClass1</exec.mainClass>
	</properties>

If run following command MainClass1 will be executed by default.

$ mvn exec:java

Above command executes MainClass1, if you want to run MainClass2, you can override default class name provide in command like following example.

$ mvn exec:java -Dexec.mainClass="com.javabydeveloper.util.MainClass2"

Results:

---- Main Class2 -----
No arguments recieved!

2. Pass arguments from command

2.1. It’s possible to pass arguments to main method from command line. To provide arguments exec.args system property.

$ mvn exec:java -Dexec.mainClass="com.javabydeveloper.util.MainClass2" -Dexec.args="Peter John Mark"

Results:

---- Main Class2 -----
Peter
John
Mark

2.2. Similarly you can pass arguments using properties in pom.xml and use mvn exec:java to run specified class in maven properties.

	<properties>
		<exec.mainClass>com.javabydeveloper.util.MainClass1</exec.mainClass>
		<exec.args>Ryan Ajshja Krish</exec.args>
	</properties>

3. Using Plugin configuration

Suppose that, you need to run main class on particular phase, it’s also possible configuring with plugin. Let’s see following example.

			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>exec-maven-plugin</artifactId>
				<version>3.0.0</version>
				<executions>
					<execution>
						<phase>test</phase>
						<goals>
							<goal>java</goal>
						</goals>
						<configuration>
							<mainClass>com.javabydeveloper.util.MainClass1</mainClass>
							<arguments>
								<argument>Hello</argument>
								<argument>Developer!</argument>
							</arguments>
						</configuration>
					</execution>
				</executions>
			</plugin>

Run Command:

$ mvn test

Results:

---- Main Class1 -----
Hello
Developer!

There are still several options, run $ mvn exec:help -Ddetail=true -Dgoal=java command to see more options.

4. Conclusion

In this quick guide, we learned how to run main method of Java class in your project with or without arguments using Maven command.

You also might interested in following Maven guides:

  1. Install Maven on Windows
  2. Install Maven on Mac
  3. Install Maven on Ubuntu
  4. Import maven project into Eclipse
  5. Maven run single test
  6. Maven set java version
  7. Maven skip tests
  8. Maven local repository

5.References

  1. Exec plugin
  2. Run Java Program

LEAVE A REPLY

Please enter your comment!
Please enter your name here