You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium/Java执行Chromedriver时遭遇io/netty/util/Timer类未找到错误求助

问题排查与解决

1. 核心错误原因:Netty依赖缺失

NoClassDefFoundError: io/netty/util/Timer 说明项目缺少Netty的netty-common或netty-all依赖。Selenium WebDriver底层依赖Netty实现网络通信,未引入对应依赖会导致类加载失败。

解决方式:

  • 若使用Maven,在pom.xml中添加依赖:
<!-- Netty核心依赖 -->
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-common</artifactId>
    <version>4.1.94.Final</version> <!-- 版本号需匹配你的Selenium版本 -->
</dependency>

或直接引入完整Netty包:

<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.94.Final</version>
</dependency>
  • 若使用Gradle,在build.gradle中添加:
implementation 'io.netty:netty-common:4.1.94.Final'
  • 手动管理依赖的话,下载对应版本的netty-common.jar,放入项目lib目录并添加到类路径。

2. 代码语法错误修正

你的代码存在几处语法问题,不修正会导致编译/运行失败:

  • System.setproperty 需改为System.setProperty(Java方法名遵循驼峰式,P大写)
  • Chromedriver 需改为ChromeDriver(类名首字母大写)
  • 路径中的反斜杠\要转义为\\,或使用正斜杠/,比如"./downloads/chromedriver.exe"
  • 执行语句不能直接放在类的根级别,必须放在方法(如main方法)内

修正后的代码示例:

package cucumber;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumTesting { // 类名遵循首字母大写规范
    public static void main(String[] args) {
        // 设置ChromeDriver路径
        System.setProperty("webdriver.chrome.driver", ".\\downloads\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        
        // 示例操作:打开网页
        driver.get("https://www.example.com");
        
        // 操作完成后关闭驱动
        driver.quit();
    }
}

3. 额外检查点

  • 确认Selenium依赖版本与Netty版本兼容,Selenium 4.x通常适配Netty 4.1.x系列
  • 检查项目类路径是否正确包含所有依赖jar包,无遗漏或重复依赖

内容的提问来源于stack exchange,提问作者Aditya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 00:05:20