How to run TestNG from command line?

TestNG allows to run the test suites from the command line (cmd). Here's a set of prerequisites that must be fulfilled in order to run a test suite from the command line −
testng.xml file should be created to define the test suites and the testing classes to execute.
testng.xml file should be created to define the test suites and the testing classes to execute.
All dependent jars should be available inside a project folder. It includes testing.jar, jcommander.jar and any other jars used in the test cases.
All dependent jars should be available inside a project folder. It includes testing.jar, jcommander.jar and any other jars used in the test cases.
Path of bin or out folder where the .class files are stored after the compilation.
Path of bin or out folder where the .class files are stored after the compilation.
Step 1 − Create different testing classes having different @Test methods
Step 1 − Create different testing classes having different @Test methods
Step 2 − Compile the class; it will create an out folder in IntelliJ and bin folder in Eclipse.
Step 2 − Compile the class; it will create an out folder in IntelliJ and bin folder in Eclipse.
Step 3 − Place all the jar files in the lib folder.
Step 3 − Place all the jar files in the lib folder.
Step 4 − Now create the testng.xml as given below.
Step 4 − Now create the testng.xml as given below.
Step 5 − Open the cmd.
Step 5 − Open the cmd.
Step 6 − Navigate to the project path using cd <project_path>
Step 6 − Navigate to the project path using cd <project_path>
Step 7 − Run the following command−
Step 7 − Run the following command−
java -cp <path of lib>; <path of out or bin folder>
org.testng.TestNG <path of testng>/testng.xml
The following code demonstrates how to run TestNG from the command line −
import org.testng.annotations.*;
import org.testng.annotations.Test;
public class OrderofTestExecutionInTestNG {
   // test case 1
   @Test
   public void testCase1() {
      System.out.println("in test case 1");
   }
   // test case 2
   @Test
   public void testCase2() {
      System.out.println("in test case 2");
   }
   @BeforeMethod
   public void beforeMethod() {
      System.out.println("in beforeMethod");
   }
   @AfterMethod
   public void afterMethod() {
      System.out.println("in afterMethod");
   }
   @BeforeClass
   public void beforeClass() {
      System.out.println("in beforeClass");
   }
   @AfterClass
   public void afterClass() {
      System.out.println("in afterClass");
   }
   @BeforeTest
   public void beforeTest() {
      System.out.println("in beforeTest");
   }
   @AfterTest
   public void afterTest() {
      System.out.println("in afterTest");
   }
   @BeforeSuite
   public void beforeSuite() {
      System.out.println("in beforeSuite");
   }
   @AfterSuite
   public void afterSuite() {
      System.out.println("in afterSuite");
   }
}
This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1">
   <test name = "test1">
      <classes>
         <class name = "OrderofTestExecutionInTestNG"/>
      </classes>
   </test>
</suite>
java -cp
C:\Users\********\IdeaProjects\TestNGProject\lib\*;C:\Users\***
*****\IdeaProjects\TestNGProjectct\out\production\TestNGProject
org.testng.TestNG src/testng.xml
If the user didn't navigate to the testing project path using cd <project path>, then the complete path can be provided in the command as shown above. But, if the user is already in the testing project path, then the command can be modified as follows −
java -cp .\lib\*;.\out\production\TestNGProject
org.testng.TestNG src\testng.xml
in beforeSuite
in beforeTest
in beforeClass
in beforeMethod
in test case 1
in afterMethod
in beforeMethod
in test case 2
in afterMethod
in afterClass
in afterTest
in afterSuite
===============================================
Suite1
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================