Build And Run Java Project With Maven

After you have learned How To Create Java Project With Maven, i think you are eagerly to know how to build and run it. In this article you can learn below tasks.

  1. How to build and run maven project.
  2. How to add your java file into maven project.
  3. Add TestNG test framework jar files in this maven project.

Build & Run Maven Project

  1. Go to the project folder, in my environment it is C:\WorkSpace\MvnExampleProject\dev2qaExample
  2. Open a command prompt and run “mvn clean package”
    clean means remove all existing java class files. Recompile all the java source file.
    package means put all the class file into a jar file.
  3. When above command finished successfully, you will see there has a target folder under the project like below.
    target folder after mvn clean package
  4. Open the target folder, you will find the jar file generated.
    packaged jar file by maven
  5. Run “java -cp ./target/dev2qaExample-1.0-SNAPSHOT.jar com.dev2qa.example.App” under project folder. You can see below output.
    run the java class create by maven
    -cp is the abbreviation of “class path” which means find the java class in the after jar file. The “Hello World” is just printed in the main method of App class.

Add Other Java File To Maven Project

  1. Create file as below. This is a util class which has one method that print out the welcome message.
    package com.dev2qa.util;
    public class AppUtil 
        public void welcomeToMavenWorld( String userName )
    	StringBuffer strBuf = new StringBuffer();
    	strBuf.append("Hello ");
    	strBuf.append(", welcom to maven world.");
            System.out.println( strBuf.toString() );
  2. Because it is in package com.dev2qa.util which is different from com.dev2qa.example, so we should save it under folder C:\WorkSpace\MvnExampleProject\dev2qaExample\src\main\java\com\dev2qa\util file folder
  3. Now you can see there are two folders under dev2qa folder, one is original example folder the other is just created util folder.
    two folders refer to two different package
  4. We also need to edit the file which exist in src/main/java/com/dev2qa/example folder to use the AppUtil class we just created.
    package com.dev2qa.example;
    import com.dev2qa.util.*;
    public class App 
        public static void main( String[] args )
            AppUtil appUtil = new AppUtil();
  5. Run “mvn -clean package” again to build and package the jar file.
  6. Run “java -cp ./target/dev2qaExample-1.0-SNAPSHOT.jar com.dev2qa.example.App” under project folder again. You will see different output by AppUtil class as below.
    output by AppUtil class

Add TestNG jar File

The default test framework for this maven project is junit. And we want use testNG to replace it.

  1. Go to You will see there are “remote repository” and dependency xml segments in below maven section.
    testng maven repository and dependency xml element
  2. Add above xml into your local pom.xml as below.
    add testng repository and dependency in pom.xml
  3. Run “mvn clean package” again. You will find maven will download testNG jar files from it’s repository and save those jars into local repository.


Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.