0

I have created a maven project and writing tests in it using testng. I have also created an executable jar using maven but getting error when executing the jar in command prompt with the arguments that are used in project. I could not actually figure out the error. Please help me with this.

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)
Caused by: java.lang.NoClassDefFoundError: org/testng/TestNG
        at mypackage.App.main(App.java:40)
        ... 5 more
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 6 more

command prompt error image in this link

My project Main class codes(App.java)

 public class App {
    public static String testdataExcel;
    public static Properties props = null;

    public static Properties loadPropertyFile(String filepath) throws IOException {
        FileInputStream stream = new FileInputStream(filepath);
        props = new Properties();
        props.load(stream);
        return props;
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        //Receiving arguments from command prompt
         String Filename = args[0];
         testdataExcel = args[1];
         
        loadPropertyFile(Filename);
        
        TestNG testng = new TestNG();       
        testng.setTestClasses(new Class[] { LoginPage.class, testclass2.class,
                testclass3.class, testclass4.class,
                testclass5.class, testclass6.class });
        
        testng.run();

     }
}

    
    

I have tried to reinstall java, testng as well and then i ran the project but i am getting same error. i could not figure out actual error so far. so kindly help me to find the error and complete this task.

Thanks in advance :)

  • As per the error looks like `org.testng.TestNG` is not getting imported. How are you creating the jar file? Make sure you included the Testng library at the moment of building the jar or you can execute the jar passing a classpath where you have the libraries. – Tavo Sanchez May 05 '21 at 07:33
  • Thanks @TavoSanchez . I have included the Testng library into the project at the moment of building the jar. I have created the jar by Rightclick->export->java->runnable jar file steps. i do not know how to get the classpath where all libraries. kindly guide me to find the classpath in eclipse. Additionally this project is running perfectly from the eclipse, but its not run in command prompt. – Praveen Kumar May 05 '21 at 08:00
  • Looks like eclipse is actually not including the dependencies into the jar. Here you can find how to do it with Maven https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven?noredirect=1&lq=1 it will create and include your dependencies in the jar file. – Tavo Sanchez May 05 '21 at 08:07
  • Thanks @TavoSanchez . now jar is running but when i run the project jar from outside the project folder { [SuiteHTMLReporter] [ERROR] test-output\testng.css (The system cannot find the path specified) } this exception occurred when I run the jar from command prompt. – Praveen Kumar May 12 '21 at 13:45

0 Answers0