title: Running TestNG via Command Line
body: <p><b>Some notes:</b><br>
  1. The TestNG jar I'm using initially is from the maven repo within our java project. All the methods below are tested with both the maven testNG jar and a fresh testNG 6.9.12 download.<br>
  2. I can run the file just file from within eclipse. Only having problems when I try to run from command line. <br>
  3. The ultimate goal is to trigger the command line to run the test via a javafx application button press. I'd appreciate it greatly if the solutions provided avoid typing absolute paths in cmd as the application is to be used on different machines, but as long as I can run it on my machine, I'll try to figure out the rest separately :)<br>
  4. From what I have seen online there are different problems encountered when trying to run a testng xml file. Please bear with me if this stack question follows a series of other questions related to running the file. Thanks in advance.</p>
  
  <p><b>Problem:</b>
  I'm trying to run a testNG "test.xml" using command line.<br>
  There are numerous guide over the web but none is working for me.</p>
  
  <p>Here are some of the things I've tried so far, among a couple of others:<br>
  <b>Method 1</b><br>
  Sources:<br>
  1. <code>https://www.safaribooksonline.com/library/view/testng-beginners-guide/9781782166009/ch02s05.html</code> <br>
  2. <code>http://stackoverflow.com/questions/11896791/how-to-run-testng-from-command-line</code>
  <li> Open CMD
  <li> CD to the folder containing the test.xml file
  <li> Syntax: <code>java -cp "[complete path to testNG jar]:[complete path to the compiled java classes to run]" org.testng.TestNG [testng xml file name]</code>
  <li> Actual CMD input I entered: <code>java -cp "C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar:C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\target\test-classes\NBS\testcases" org.testng.TestNG test.xml</code></p>
  
  <p>Result:<br></p>
  
  <pre><code>Error: Could not find or load main class org.testng.TestNG
  </code></pre>
  
  <p>I also tried replacing the <code>[complete path to testNG jar]</code> with "." like <code>java -cp ".:C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\target\test-classes\NBS\testcases" org.testng.TestNG test.xml</code> as suggested on the 2nd source link above and got the same error.</p>
  
  <p><b>Method 2</b><br>
  Source:<br>
  1. <code>https://www.tutorialspoint.com/testng/testng_environment.htm</code>
  <li> Set environment variable: <code>TESTNG_HOME = C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\org\testng\testng\6.9.10</code>
  <li> Set environment variable: <code>CLASSPATH = %CLASSPATH%;%TESTNG_HOME%\testng-6.9.10.jar</code>
  <li> CD to the folder containing the test.xml file
  <li> Syntax: <code>java -cp "[complete path to the compiled java classes to run]" org.testng.TestNG [testng xml file name]</code>
  <li> Actual input: <code>java -cp "C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\target\test-classes\NBS\testcases" org.testng.TestNG test.xml</code></p>
  
  <p>Result:<br></p>
  
  <pre><code>Error: Could not find or load main class org.testng.TestNG
  </code></pre>
  
  <p><b>Method 3</b><br>
  Source:<br>
  1. <code>http://stackoverflow.com/questions/28303723/could-not-find-or-load-main-class-org-testng-testng</code>
  <li> CD to the folder containing the test.xml file
  <li> Enter: <code>java -cp ".;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar" org.testng.TestNG test.xml</code></p>
  
  <p>Result:<br>
  <a href="https://i.stack.imgur.com/tLLom.png" rel="nofollow noreferrer">java.lang.NoClassDefFoundError: com/beust/jcommander/
  ParameterException</a></p>
  
  <p>Then I tried to troubleshoot method 3 based on comments from <code>http://stackoverflow.com/questions/7100821/suddenly-cant-run-testng-tests-from-ant-testng-caused-by-java-lang-classnot</code>
  <li>Download a fresh testNG jar file. Results still same.
  <li>Add jcommander.jar in classpath: <code>java -cp ".;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\com\beust\jcommander\1.48\jcommander-1.48.jar" org.testng.TestNG test.xml</code></p>
  
  <p>Result: </p>
  
  <pre><code>[TestNG] [ERROR]
  Cannot find class in classpath: NBS.testcases.LoginWithBayanUser
  </code></pre>
  
  <p><li> Seeing the error above, I added the compiled java classes path in classpath: <code>java -cp ".;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\com\beust\jcommander\1.48\jcommander-1.48.jar;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\target\test-classes\NBS\testcases" org.testng.TestNG test.xml</code></p>
  
  <p>Result: </p>
  
  <pre><code>[TestNG] [ERROR]
  Cannot find class in classpath: NBS.testcases.LoginWithBayanUser
  </code></pre>
  
  <p><b>To summarize:</b><br>
  I think the closest attempt I got was after troubleshooting method 3 but still unable to run the xml. The same xml runs from within eclipse. </p>
  
  <p>Command breakdown:
  Full: <code>java -cp ".;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\com\beust\jcommander\1.48\jcommander-1.48.jar;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\target\test-classes\NBS\testcases" org.testng.TestNG test.xml</code></p>
  
  <p>1st part: <code>java -cp</code> <br>
  2nd part: <code>"[.];[full path to testNG jar];</code> <br>
  3rd part: <code>"[full path to jcommander jar];</code> <br>
  4th part: <code>"[full path to compiled java classes]"</code> <br>
  5th part: <code>org.testng.TestNG [testNG xml file name]</code> <br></p>
  
  <p>Command executed on [directory of testNG xml file]</p>
  
  <p>Please help! Thanks in advance!</p>
  
  <p>=======================================================<br>
  <b>EDIT 1:</b><br>
  I removed extra folders from the compiled java classes folder as suggested by Julien Herr and it produced a different result. I think I'm getting closer but still not able to run.</p>
  
  <pre><code>[TestNGClassFinder] Warning: Can't link and determine methods of class NBS.testcases.LoginWithBayanUser
  [TestNGClassFinder] Warning: Can't link and determine methods of class NBS.testcases.LoginWithBayanUser2
  [[TestNGClassFinder]] Unable to read methods on class NBS.testcases.LoginWithBayanUser - unable to resolve class reference Automation/framework/dataModel/CaseInfo
  [[TestNGClassFinder]] Unable to read methods on class NBS.testcases.LoginWithBayanUser2 - unable to resolve class reference Automation/framework/dataModel/CaseInfo
  [TestNG] Running:
    C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\target\test-classes\testngRunner\test.xml
  
  
  ===============================================
  Suite1
  Total tests run: 0, Failures: 0, Skips: 0
  ===============================================
  </code></pre>
  
  <p>Here's the contents of my test.xml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"&gt;
  &lt;suite name="Suite1" verbose="1" parallel="classes" thread-count="2" preserve-order="true"&gt;
      &lt;test name="Regression suite 1" &gt;
          &lt;parameter name="globalXlsDataIndex" value="1"/&gt; 
          &lt;classes&gt;
              &lt;class name="NBS.testcases.LoginWithBayanUser" /&gt;
              &lt;class name="NBS.testcases.LoginWithBayanUser2" /&gt;
          &lt;/classes&gt;
      &lt;/test&gt;
  &lt;/suite&gt;
  </code></pre>
  
  <p>To reiterate, the same xml file runs from within eclipse.</p>
  
  <p>Thanks!</p>
  
  <p>=======================================================<br>
  <b>EDIT 2:</b><br>
  I added <code>C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\lib\*</code> in the classpath where a compiled jar containing the <code>Automation/framework/dataModel/CaseInfo</code> is located. Now I'm getting another error: </p>
  
  <pre><code>[TestNG] [ERROR]
  Cannot instantiate class NBS.testcases.LoginWithBayanUser
  </code></pre>
  
  <p>Hope you can help! Thanks!</p>