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

IntelliJ中运行Cucumber报NoClassDefFoundError问题求助(附Gradle配置)

Fixing NoClassDefFoundError When Running Cucumber with Gradle in IntelliJ

Hey there, let's work through this NoClassDefFoundError you're hitting. From the Gradle config snippets you shared, there are a few key fixes that should resolve the issue:

1. Complete Your Cucumber Dependencies

Your dependencies block cuts off at testCompile 'io.cucumber:c...'—this is almost certainly missing core Cucumber libraries needed at runtime. Here's a complete set of compatible dependencies (match versions to your gherkin 5.0.0; gherkin 5.x works well with Cucumber 5.x/6.x):

dependencies {
    compile 'org.apache.commons:commons-lang3:3.7'
    testCompile 'io.cucumber:gherkin:5.0.0'
    testCompile 'io.cucumber:cucumber-java:5.7.0' // Match this to your gherkin version
    testCompile 'io.cucumber:cucumber-junit:5.7.0'
    testCompile 'junit:junit:4.13.2' // Required for Cucumber-JUnit integration
}

Make sure all versions are compatible—mismatched versions often lead to missing class errors.

2. Verify the Cucumber Main Class Path

Older Cucumber versions use cucumber.api.cli.Main, but newer versions (5+) moved the main class to io.cucumber.core.cli.Main. If you're using a newer Cucumber version but referencing the old main class, you'll get a NoClassDefFoundError. Update your task:

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main" // Update this line for newer Cucumber versions
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'gradle.cucumber', 'src/test/resources']
        }
    }
}

3. Refresh Gradle Dependencies and Rebuild

IntelliJ sometimes fails to sync Gradle dependencies properly. Run these commands in your terminal to ensure all dependencies are downloaded and classes are compiled:

./gradlew clean build --refresh-dependencies

After this, go to the Gradle panel in IntelliJ and click "Refresh all Gradle projects" to make sure the IDE picks up the changes.

4. Double-Check Your Glue Path

While less likely to cause NoClassDefFoundError, confirm that your --glue value (gradle.cucumber) matches the actual package where your Cucumber step definition classes are located. A mismatched path won't throw a class not found error, but it's a good sanity check.

Once you've made these changes, try running the cucumber task again from Gradle (either via terminal or IntelliJ's Gradle panel). This should resolve the missing class issue.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:30:40