如何解决ClassNotFoundException:com.beust.jcommander.ParameterException

时间:2019-07-03 20:28:53

标签: java maven selenium

我是JAVA开发的新手,在Eclipse中编译类时遇到以下问题。 编译时的错误如下:

“ Java虚拟机启动器 错误:发生了JNI错误,请检查您的安装,然后重试。“

错误如下:

“ Java虚拟机启动器 发生了Java异常。“

在控制台中,出现的错误如下:

*Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException
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: com.beust.jcommander.ParameterException
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*

有人可以帮助我吗? 感激。

AppTest.java

package all_Exercices.Different_Browsers;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Parameters;


public class AppTest
{
    WebDriver driver;

    @Parameters("browser")
    public void verifypageTitle(String browserName) 
    {
        if(browserName.equalsIgnoreCase("firefox"))
        {
            driver = new FirefoxDriver();
        }
        else if(browserName.equalsIgnoreCase("chrome"))
        {
            System.setProperty("webdriver.chrome.driver", "C:\\SeleniumGecko\\chromedriver.exe");
            driver = new ChromeDriver();
        }
        else if(browserName.equalsIgnoreCase("IE"))
        {
            System.setProperty("webdriver.ie.driver", "C:\\SeleniumGecko\\IEDriverServer.exe");
            driver = new InternetExplorerDriver();
        }

        driver.manage().window().maximize();
        driver.get("https://shopcart-challenge.4all.com/");
        System.out.println(driver.getTitle());
        driver.quit();
    }
}

crossbrowser.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
  <test name="FirefoxTestCases">
  <parameter name="browser" value="firefox"/>
  <classes>
    <class name="all_Exercices.Different_Browsers.AppTest"/>
  </classes>    
  </test> <!--Test -->
    <test name="ChromeTestCases">
  <parameter name="browser" value="chrome"/>
  <classes>
    <class name="all_Exercices.Different_Browsers.AppTest"/>
  </classes>    
  </test> <!--Test -->
    <test name="IETestCases">
  <parameter name="browser" value="ie"/>
  <classes>
    <class name="all_Exercices.Different_Browsers.AppTest"/>
  </classes>    
  </test> <!--Test -->
</suite> <!--Suite -->

POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>4all_Exercices</groupId>
  <artifactId>Different_Browsers</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Different_Browsers</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

1 个答案:

答案 0 :(得分:0)

什么原因导致错误

如果抛出java.lang.ClassNotFoundExceptionjava.lang.NoClassDefFoundError异常,则表示JVM在类路径中找不到消息中提到的类(此处为com.beust.jcommander.ParameterException)。 / p>

程序的任何部分,很可能是测试启动器(TestNG,或者在您的情况下为JUnit),似乎都在使用命令行解析器 Cedric Beust JCommander

如何解决

在构建类路径中添加所需的类(在本例中为整个jar)将使JVM查找并加载它。由于您拥有Maven项目,因此可以简单地在dependencies元素/标签内将依赖项添加到POM.xml

<dependency>
  <groupId>com.beust</groupId>
  <artifactId>jcommander</artifactId>
  <version>1.71</version>
</dependency>

请参见JCommander Download section

另请参见

这个问题描述了一个类似的问题:Getting "java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException"Exception