how to call testng.xml from java main method?

You can run testng directly from commandline, probably make a bat file on top of it or use jenkins to trigger it. Refer here

or

If you want it in main, then you can try

TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
List<String> suites = Lists.newArrayList();
suites.add("c:/tests/testng1.xml");//path to xml..
suites.add("c:/tests/testng2.xml");
testng.setTestSuites(suites);
testng.run();

All the answers above works like a charm but there is one limitation that it would run unless and untill you remove the scope tag in pom.xml

In pom.xml there would be maven dependancy for testNg and in there there would be one scope tag which limits our jar to only upto test

Just remove that one


Please try below code and make sure you have testNG jar added in your jar manifest file.

public static void main(String[] args)
{
    org.testng.TestNG.main(args);
}

now you can pass all the parameters to your jar which are same for testNG jar file.

e.g.

java -jar yourjar.jar testng.xml

or

java -jar yourjar.jar -testclass org.test.xyz.java


This work for me. More details here.

// Create object of TestNG Class
TestNG runner=new TestNG();

// Create a list of String 
List<String> suitefiles=new ArrayList<String>();

// Add xml file which you have to execute
suitefiles.add("C:\\Automation Test\\Git\\vne_automation\\testng.xml");

// now set xml file for execution
runner.setTestSuites(suitefiles);

// finally execute the runner using run method
runner.run();

Hope this helps!