How to create executable JAR for TestNG class using intellij and gradle?

Problem:- How to create a executable JAR for testng class using intellij + gradle.

Tool stack :– Intellij (IDE) + Gradle (Build tool) + TestNg (Testing framework)

Share more learn more  FbG+,Twitter 

GITHUB for complete code

Step1:– Create a basic java class to display “HelloBuild!”, execution using TestNG.

public class TestBuild {
public void test() {

Step2:-Browse to the parent directory of the java class using Terminal/Command prompt > gradle clean 

This is used to clean any existing build directory (cleaning everything including leftovers from previous builds which are no longer relevant. )

Step3:- gradle build, build the java projects and create the JAR package inside build > libs > test-****.jar

Step4:- Now our jar package is ready lets browse to this package and try to run the java class
java – jar test-****.jar

This failed with “no main manifest…”

Step5:-add the below line to your build.gradle

jar {
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
manifest {
attributes 'Main-Class': 'TestBuild'

Main-Class : ‘Name of the class to be build

Step6.:- if we repeat step2 > step 4 again, this time, you will get the “no main method error” as below. As our code doesn’t contain any main method to start the execution.

Step7:- Here comes the final solution…Adding the below code inside our main method in the “TestBuild” class and add the testng.xml file containing the TestBuild class name as below.

Now repeat the build process above, this time it will execute the code and print the “HelloBuild!”


Main Method:-

    public static void main(String[] args) {
TestNG testng = new TestNG();
List suites = Lists.newArrayList();

testng.xml file:-

<!DOCTYPE suite SYSTEM "" >
<suite name="BuildTest">
<test name="FirstTest">
<class name="TestBuild"/>

Learn More JAVA 
Learn Automation

Leave a Reply

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