java - 错误 : cannot find symbol in gradle compileTestJava

标签 java gradle build.gradle

我收到了很多 error: cannot find symbol compileTestJava 任务在我的 Gradle 项目中运行时出错。

我的代码在 src/main/java我的测试在src/test/java并且测试类无法解析 src/main/java 中的任何类

例如。:

C:\q\src\test\java\q\MainTest.java:14: error: cannot find symbol
            Main.main(new String[0]);
            ^
  symbol:   variable Main
  location: class MainTest

但是q\src\main\java\q\Main.java存在,编译并且是同一包中的公共(public)类。

构建.gradle:
plugins {
    id 'java'
}

repositories {
    jcenter()
}

dependencies {
    implementation 'com.google.guava:guava:29.0-jre'

    testImplementation 'junit:junit:4.13'
}

我一直在通过将以下内容添加到 build.gradle 来解决这个问题:
sourceSets {
       test {
               java.srcDirs = ['src/main/', 'src/test/']
       }
}

这有效,但它会导致 IntelliJ 中的错误。

最佳答案

我有同样的问题,同样的修复确实有效。
我还设法让它在没有修复的情况下工作。就我而言,问题是由项目的路径引起的。它嵌套在名称中带有冒号 (:) 的目录中。也许还要寻找任何其他违规行为(分号、空格、...)。

关于java - 错误 : cannot find symbol in gradle compileTestJava,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61707087/

相关文章:

java - 导出包含超过 80.000 条记录的 XLS

java - Java 中的服务器客户端 : could not start client

android - 尝试将 gradle 与新库同步时出错

spring-mvc - SpringBoot1.4-我运行IntegrationTest时无法找到@SpringBootConfiguration,在测试错误中使用@ ContextConfiguration/@SpringBootTest(class)

android - 在库模块中使用 Kotlin,而不在 app 模块中使用它

android - 如何在通过 Jenkins 构建 apk 时动态设置 buildConfigField 参数

android - 错误 : In project 'app' a resolved Google Play services library dependency depends on another at an exact version

java - Android后台HTTP请求不起作用?

java - 查找包含字符串的最短可能子字符串

android - 从 Gradle 命令访问项目相关数据