Gradle compileJava 无法从源文件中找到符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28174433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:43:52  来源:igfitidea点击:

Gradle compileJava cannot find symbol from source file

javaeclipsegradlewar

提问by JorganPubshire

I'm new with Gradle, so there's a chance that I am missing something obvious here. I am working on using Gradle for automated building of our projects. I've tackled most of them, but one is causing me unending trouble. Since this is for my company, I can't share any real code, but I will do my best to provide realistic pseudo-code.

我是 Gradle 的新手,所以我有可能在这里遗漏了一些明显的东西。我正在使用 Gradle 自动构建我们的项目。我已经解决了其中的大部分问题,但其中一个给我带来了无休止的麻烦。由于这是我公司的,我不能分享任何真实的代码,但我会尽力提供真实的伪代码。

I am attempting to compile a web application with a flex front-end and java back-end. Right now, I am focused solely on creating a .war file with the java in it, then I will move into the flex compilation. When I run the following build.gradle script, it gives me countless "cannot find symbol" errors.

我正在尝试使用 flex 前端和 java 后端编译 Web 应用程序。现在,我只专注于创建一个包含 java 的 .war 文件,然后我将进入 flex 编译。当我运行以下 build.gradle 脚本时,它给了我无数的“找不到符号”错误。

apply plugin: 'eclipse'
apply plugin: 'war'

sourceCompatibility = 1.6

repositories {
   mavenCentral()
}

sourceSets.main.java.srcDir 'src'
sourceSets.main.resources.srcDir 'src'

webAppDirName = "WebContent"

war.archiveName "Gradle_test.war"

dependencies {
    compile fileTree(dir: 'WebContent/WEB-INF/lib', include: '*.jar')
    providedCompile 'org.apache.tomcat:catalina:6.0.13'
    providedCompile 'org.apache.tomcat:dbcp:6.0.13'
}

An example of the errors that were occurring (with fake package and class names):

发生的错误示例(使用假包名和类名):

/Users/user/Documents/eclipse/ProjectName/src/com/companyName/teamName/projectName/core/release/subProjectName/projectVersion/RenamedObject.java:385: cannot find symbol
symbol  : class OtherObject
location: class com.companyName.teamName.projectName.core.release.subProjectName.projectVersion.RenamedObject
    public class InnerClass extends OtherObject implements Indexable<String>, Serializable {

The class InnerClass is declared within the RenamedObject.java file along with the RenamedObject class. The symbol it cannot find, OtherObject, is not from a .jar. It is a source file stored at /Users/user/Documents/eclipse/ProjectName/src/com/companyName/teamName/projectName/core/release/subProjectName/projectVersion/superTypes/OtherObject.java. The object is imported at the top of RenamedObject.java and can be built and run properly via eclipse without any special settings. These errors occur during the :compileJava task after executing gradle warfrom terminal.

类 InnerClass 与 RenamedObject 类一起在 RenamedObject.java 文件中声明。它找不到的符号OtherObject 不是来自.jar。它是一个存储在/Users/user/Documents/eclipse/ProjectName/src/com/companyName/teamName/projectName/core/release/subProjectName/projectVersion/superTypes/OtherObject.java. 该对象在 RenamedObject.java 的顶部导入,无需任何特殊设置即可通过 eclipse 正常构建和运行。gradle war从终端执行后,在 :compileJava 任务期间会发生这些错误。

Is there any reason that Gradle would have an issue with handling source imports from a sub-package like this? Am I just doing something wrong? Let me know if there is any more info that can help and I will provide it if I can.

Gradle 是否有任何理由在处理来自这样的子包的源导入时出现问题?我只是做错了什么吗?如果有更多信息可以提供帮助,请告诉我,如果可以,我会提供。

EDIT: I got it to work. All 130 of the errors were resolved with one change. If I add the fully qualified package name to OtherObject in the extends, so it looks like this:

编辑:我让它工作。通过一次更改,所有 130 个错误都得到了解决。如果我将完全限定的包名称添加到扩展中的其他对象,则它看起来像这样:

public class InnerClass extends com.companyName.teamName.projectName.core.release.subProjectName.projectVersion.superTypes.OtherObject implements Indexable<String>, Serializable {

That one change made it all work, but I still don't understand the reason. That is not the only reference to OtherObject in the project and it is not the only place that an inner class extends a class defined in another source package.

那个改变使它一切正常,但我仍然不明白原因。这不是项目中对 OtherObject 的唯一引用,也不是内部类扩展另一个源包中定义的类的唯一地方。

回答by acthota

Symptoms

症状

Could not execute build using Gradle distribution 'https://services.gradle.org/distributions/gradle-2.8-bin.zip'

无法使用 Gradle 分发版执行构建 ' https://services.gradle.org/distributions/gradle-2.8-bin.zip'

... .... .....

………………

Caused by: org.gradle.api.internal.tasks.compile.CompilationFailedException: Compilation failed; see the compiler error output for details.

引起:org.gradle.api.internal.tasks.compile.CompilationFailedException:编译失败;有关详细信息,请参阅编译器错误输出。

The Gradle Build Reports that you should check the compiler Java Output.

Gradle Build 报告您应该检查编译器 Java 输出。

Check the output and see which class name it cites in the output which it is unable to resolve.

检查输出并查看它在无法解析的输出中引用了哪个类名。

symbol:  class <referenced_classname_here
location: class <parent_classname_here

Solution

解决方案

Provide the Fully Qualified Domain Name wherever you reference the class that is mentioned as the symbolnot resolved.

在您引用作为未解析符号提及的类的任何地方提供完全限定域名。

In my case, I have seen this when I defined an Inner Classwhich extends a class in a library that I had declared as a dependency in my build.gradle.

就我而言,当我定义一个内部类时,我已经看到了这一点,该类扩展了我在 build.gradle 中声明为依赖项的库中的类。

P.S:

PS:

I have seen that the solution for this has already been mentioned as a comment to the question by the OP.

I could not find this question since it did not have the right keywords for google and also there was no accepted solution which further reduced its visibility.

The result of this is ONE FULL DAY WASTED, several strands of hair plucked out before I realized this stupid hackish solution :( :(

我已经看到 OP 已经将解决​​方案作为对问题的评论提到了。

我找不到这个问题,因为它没有正确的 google 关键字,而且也没有公认的解决方案,这进一步降低了它的可见性。

结果浪费了一整天,在我意识到这个愚蠢的黑客解决方案之前拔掉了几缕头发:(:(

I have tried everything under the Sun that is remotely related to dependency resolution to fix this "dependency issue" which was never an issue with my codebase.

我已经在 Sun 下尝试了所有与依赖解析相关的东西来解决这个“依赖问题”,这在我的代码库中从来都不是问题。

Hope the Gradle Team fixes this ASAP before more people pluck their hair.

Btw... I Love Gradle. :)

希望 Gradle 团队在更多人拔头发之前尽快解决这个问题。

顺便说一句......我喜欢Gradle。:)

回答by Ray Tayek

Just adding to the knowledge base, but I had just the same type of problem with:

只是添加到知识库中,但我遇到了相同类型的问题:

public static class GetNetworkInterfacesCallable implements java.util.concurrent.Callable<java.util.Set<java.net.InetAddress>> {
    public GetNetworkInterfacesCallable(String networkPrefix) {
        this.networkPrefix=networkPrefix;
    }
    @Override public Set<InetAddress> call() throws Exception {
        Thread.currentThread().setName(getClass().getName());
        return null; //myInetAddresses(networkPrefix);
    }
    final String networkPrefix;
}

Using gradle 2.9 and 2.10.

使用 gradle 2.9 和 2.10。

回答by Vokail

I had same problem here today. If you are working with gradle, be sure your class is in src/main/java not src/test/java otherwise your IDE (for example Eclipse) will say that all compiles find, but when you try to build with gradle, test classes are not included by default (and it's okay!), so it will not compile.

我今天在这里遇到了同样的问题。如果您正在使用 gradle,请确保您的类在 src/main/java 中而不是 src/test/java 否则您的 IDE(例如 Eclipse)会说所有编译都找到了,但是当您尝试使用 gradle 构建时,测试类默认情况下不包括在内(没关系!),所以它不会编译。