run main class of Maven project
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Apache Maven is a powerful project management and comprehension tool that uses a project object model (POM) to manage project builds, dependencies, and documentation. One of the key capabilities that Maven provides is the ability to compile and run Java classes directly, including the main class of a project, which often serves as the entry point of Java applications. This article guides you through the process of running the main class of a Maven project with detailed explanations and examples.
Understanding Maven's Project Structure
Before diving into running the main class, it is essential to understand the standard directory layout of a Maven project:
src/main/java: This directory contains the project's Java source files.src/main/resources: This directory holds resources necessary for the Java source files, like configuration files.src/test/java: This directory contains the test source files.src/test/resources: Here, you'll find resources for the test source files.pom.xml: The project object model file which contains all configurations and dependencies required by the project.
Configuring the POM.xml
The pom.xml is central to the Maven project. It defines project dependencies along with other configurations necessary for building the project. To run a Java class with a main method, Maven needs the maven-exec-plugin. Here's an example configuration that you should add within the <plugins> section of your pom.xml file:
Replace com.example.MainClass with the fully qualified name of your main class.
Running the Main Class
To run the main class using Maven, use the exec:java goal from the command line:
This command triggers Maven to execute the main class specified in the pom.xml file.
Checking for Common Issues
When running Java applications via Maven, several common issues may arise:
- Dependencies: Ensure all necessary dependencies are correctly listed in your
pom.xml. - Plugin Configuration: Verify that the
exec-maven-pluginis correctly configured. - Main Class Name: Ensure that the main class name and package are correctly specified.
Practical Example: Hello World Application
Let’s create a simple "Hello, World!" Java application to demonstrate running a main class through Maven.
- Setup Project Structure: Arrange your files following Maven’s standard directory layout.
- Create the
MainClass.java:
- Update the POM.xml as shown in the previous example section.
- Build and Run: Navigate to your project directory in the terminal and execute:
This will compile your code and run the main method of MainClass.
Summary Table
Below is a table summarizing the key points about each section discussed:
| Section | Key Points |
| Project Structure | Source and resource directories explained |
| POM Configuration | exec-maven-plugin setup |
| Running the Main Class | Use mvn exec:java command |
| Common Issues | Dependency, plugin configuration, main class name |
| Example Application | Demonstrated a simple "Hello, World!" application |
By following these guidelines and using the provided configurations and examples, developers can efficiently execute the main class of a Maven-based Java project, providing a streamlined approach to both development and debugging processes.
Related reading
- Run mvn spring-bootrun from parent module?
- Run Spring-boot's main using IDE
- run spring batch job from the controller
- Runnable with a parameter?
- Running a MVC app using Spring Boot Hibernate MySql
- Running chron job on a single instance of spring service deployed on aws
- Running code after Spring Boot starts
- Running code after Spring Boot starts

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.