title: Run testNG programatically in Eclipse using maven without a plugin
body: <p>I want to run TestNG without using the "TestNG eclipse plugin" in a normal maven quickstart project.</p>
  
  <p>Whatever I read from <a href="http://testng.org/doc/documentation-main.html#running-testng-programmatically" rel="nofollow noreferrer">http://testng.org/doc/documentation-main.html#running-testng-programmatically</a> &amp; other sources, I got idea of using classes as:</p>
  
  <p><strong>MessageUtil.java</strong></p>
  
  <pre><code>package com.mytests.testng;
  
  public class MessageUtil {
     private String message;
  
     public MessageUtil(String message){
        this.message = message;
     }
  
     public String printMessage(){
        System.out.println(message);
        return message;
     }   
  }
  </code></pre>
  
  <p><strong>TestNGExample.java</strong></p>
  
  <pre><code>package com.mytests.testng;
  
  import org.testng.Assert;
  import org.testng.annotations.Test;
  
  public class TestNGExample {
     String message = "Hello World";  
     MessageUtil messageUtil = new MessageUtil(message);
  
     @Test
     public void testPrintMessage() {   
        Assert.assertEquals(message,messageUtil.printMessage());
     }
  }
  </code></pre>
  
  <p><strong>App.java</strong></p>
  
  <pre><code>package com.mytests.testng;
  
  import org.testng.TestListenerAdapter;
  import org.testng.TestNG;
  
  public class App
  {
      public static void main( String[] args )
      {
          System.out.println( "Hello World!" );
          TestListenerAdapter tla = new TestListenerAdapter();
          TestNG testNG = new TestNG();
  
          @SuppressWarnings("rawtypes")
          Class[] testClasses = new Class[]{
                  TestNGExample.class
          };
  
          testNG.setTestClasses(testClasses);
          testNG.addListener(tla);
          testNG.run();
      }
  }
  </code></pre>
  
  <p>But here I am getting exceptions as:</p>
  
  <p><strong>Exception Box 1</strong></p>
  
  <p><img src="https://i.stack.imgur.com/2wvOE.jpg" alt="JNI Error"></p>
  
  <p><strong>Exception Box 2</strong></p>
  
  <p><img src="https://i.stack.imgur.com/Jxdmb.jpg" alt="Java Exception"></p>
  
  <p><strong>Exception in Console</strong></p>
  
  <pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/ITestListener
      at java.lang.Class.getDeclaredMethods0(Native Method)
      at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
      at java.lang.Class.privateGetMethodRecursive(Unknown Source)
      at java.lang.Class.getMethod0(Unknown Source)
      at java.lang.Class.getMethod(Unknown Source)
      at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
      at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
  Caused by: java.lang.ClassNotFoundException: org.testng.ITestListener
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      ... 7 more
  </code></pre>
  
  <p>I am not getting why there is a problem even though I don't get any compile time error. The TestNG dependency is successfully downloaded and helps in writing the code. But the class doesn't load on run time I guess. What can be done?</p>
  
  <p>If not using any XML is causing an error, I can use it as well, but not the eclipse plugin for TestNG.</p>
  
  <p>P.S.: I am new to TestNG and this is my first program</p>