首页 > 解决方案 > 在命令提示符下执行 java runnable jar 时出错。错误:reflect.invocationtargetexception

问题描述

我创建了一个 Maven 项目并使用 testng 在其中编写测试。我还使用 maven 创建了一个可执行 jar,但是在命令提示符下使用项目中使用的参数执行 jar 时出现错误。我实际上无法弄清楚错误。请帮我解决一下这个。

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

此链接中的命令提示错误图像

我的项目主类代码(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();

     }
}

    
    

我也尝试过重新安装java、testng,然后我运行了这个项目,但我得到了同样的错误。到目前为止,我无法弄清楚实际错误。所以请帮我找出错误并完成这项任务。

提前致谢 :)

标签: javaselenium-webdriverautomationtestngexecutable-jar

解决方案


推荐阅读