gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:32:21 27 4
gpt4 key购买 nike

我是 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>

最佳答案

导致错误的原因

如果抛出 java.lang.ClassNotFoundExceptionjava.lang.NoClassDefFoundError 异常,则意味着消息中提到的类(此处:com JVM 在类路径上找不到 .beust.jcommander.ParameterException)。

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

如何解决

将所需的类(在本例中为整个 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

关于java - 如何解决ClassNotFoundException : com. beust.jcommander.ParameterException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56877529/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com