Q:

Selenium TestNG: How can I run selenium tests from a compiled jar using TestNG?

We usually fire our Selenium tests using the TestNG xml runner.
Sample XML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="P1_jobSeeker" parallel="classes" preserve-order="true" thread-count="1">
  <test name="P1_jobSeeker-1">
    <classes>
      <class name="pamr.testCases.jobSeeker.PAMRAutoJS12Part1"/>
      <class name="pamr.testCases.jobSeeker.PAMRAutoJS12Part2"/>
      <class name="pamr.testCases.jobSeeker.PAMRAutoJS12Part3"/>
    </classes>
  </test>
</suite>

Now I'd like to do this without the source files, outside the IDE and just run everything from a complied jar file.
I found this SO post and was trying out this solution from the TestNG site.
I have created my main class and indicated the classes to run.
import java.util.ArrayList;
import java.util.List;

import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class Main {
    public static void main(String[] args) {
        XmlSuite suite = new XmlSuite();
        suite.setName("P1_jobSeeker");

        XmlTest test = new XmlTest(suite);
        test.setName("P1_jobSeeker-1");
        List<XmlClass> classes = new ArrayList<XmlClass>();
        classes.add(new XmlClass("pamr.testCases.jobSeeker.PAMRAutoJS12Part1"));
        classes.add(new XmlClass("pamr.testCases.jobSeeker.PAMRAutoJS12Part2"));
        classes.add(new XmlClass("pamr.testCases.jobSeeker.PAMRAutoJS12Part3"));
        test.setXmlClasses(classes);

        List<XmlSuite> suites = new ArrayList<XmlSuite>();
        suites.add(suite);
        TestNG tng = new TestNG();
        tng.setXmlSuites(suites);
        tng.run();
    }
}

Before exporting the project to a jar, I tried running main but I'm getting a java.lang.ClassNotFoundException at this line: XmlSuite suite = new XmlSuite();
full console stack: 
Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/xml/XmlSuite
    at Main.main(Main.java:11)
Caused by: java.lang.ClassNotFoundException: org.testng.xml.XmlSuite
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more
ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):  [util.c:840]

It's probably worth noting that my project is a maven project so main and test folders are separated. 
Thanks in advance!

A:

I had a similar requirement wherein I had to run the tests as testng jar and not through the maven test phase.  Steps documented here.
HTH

