Maven“找不到符号”的信息没有帮助

这是一个非常简单的问题,可能是一个我不知道的设置,但是 Google 在这个问题上特别没有帮助,给出的结果是关于编译错误的,而不是如何更改编译错误消息。

当我使用 maven 构建我的项目时,它会给我大致格式如下的错误消息:

[ ERROR ]/path/to/source/Main.java: [13,8] ERROR: can not find sign [13,8]错误: 无法找到符号

当我使用 ant 或 javac 构建时,它实际上会告诉我在错误消息中找不到的符号。Maven 给我一个行号和字符位置,但显示实际的符号会更有帮助。上面的行是为每个“无法找到符号”错误提供的唯一行。没有上面或下面的线给出符号。我想一定有办法让 Maven 告诉我这些信息,但我不知道是什么。我尝试了-e 选项,因为 mvn 告诉我尝试使用它,但它为错误提供了一个 maven 回溯,而不是实际的符号。

有人帮忙吗?

下面是 mvn —— version 的输出

Apache Maven 3.0.4 (rNON-CANONICAL_2012-10-24_11-25_mockbuild; 2012-10-24 07:25:04-0400)
Maven home: /usr/share/maven
Java version: 1.7.0_09-icedtea, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.9.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.6.6-1.fc17.x86_64", arch: "amd64", family: "unix"

下面是一个示例(无用的)错误消息,与 maven 的输出完全一样(只是缩短了目录) :

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /path/to/source/SoundEngineFilePanel.java:[33,8] error: cannot find symbol
[ERROR]  class SoundEngineFilePanel
/path/to/source/SoundEngineFilePanel.java:[36,8] error: cannot find symbol
[INFO] 2 errors
[INFO] -------------------------------------------------------------

它找不到的符号是“ fakeThing”和“ fakeThings 2”,而不是 SoundEngineFilePanel。

205016 次浏览

This is not a function of Maven; it's a function of the compiler. Look closely; the information you're looking for is most likely in the following line.

My guess the compiler is complaining about an invalid annotation. I've noticed that Eclipse doesnt show all errors, like a comma at the end of an array in a annotation. But the standard javac does.

This is a bug in the Maven compiler plugin, related to JDK7 I think. Works fine with JDK6.

update to 3.1 :

  <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>

Even I am using Java 7, maven 2.2.1 and was getting the same error, I removed <scope>tests</scope> from my pom and used

mvn clean -DskipTests=true install to successfully build my projects, without upgrading my maven version.

I had the same problem. The reason was that I had two JAR files were not added through the Maven dependency, so when I ran mvn compile, the console display the error error:

Symbol cannot be found,Class...".

To fix it:

  1. Remove the JAR files from build path
  2. Add to build path
  3. Run mvn compile

In my case the problem was in a child jar which was not rebuilt since I have added a new class, pom.xml of that child jar was not related to my failed pom.xml as child-to-parent relation (using <parent> tag). So I rebuilt the child jar after which the error had gone.

I was getting a similar problem in Eclipse STS when trying to run a Maven install on a project. I had changed some versions in the dependencies of my pom.xml file for that project and the projects that those dependencies pointed to. I solved it by running a Maven install on all the projects I changed and then running install on the original one again.

I had the same issue with maven. It happens that my problem was, maven was generating the folders with differenc case names. I was expecting a .service.MyFile but in the target folder it was Service/MyFile and java is a case sensitive. It took me a few hours to find out, recommend you to check it out.

SOLUTION: @Before building your component (using mvn clean install). Build the entire project once and build your component again

WHY SO :
I get this error many times. Most of the times I will try to build my component alone (As I have not made changes elsewhere).

Right, But that extra jar which has been downloaded recently might have affected by changes done by a third party(inside their component). Making a full mvn clean install on entire project saved me many times

if you are having dependency on some other project in work space and these projects are not build properly, such error might come. try building such dependent projects first, it may help

This occurs because of this issue also i.e repackaging which you defined in POM file.

Remove this from pom file under maven plugin. It will work

<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>

Generally, this error will appear when your compile code's version is different from your written code's. For example, write code that rely on xxx-1.0.0.jar that one class has method A, but the method changed to B in xxx-1.1.0.jar. If you compile code with xxx-1.1.0.jar, you will see the error.

In my case, I was using a dependency scoped as <scope>test</scope>. This made the class available at development time but, by at compile time, I got this message.

Turn the class scope for <scope>provided</scope> solved the problem.

Perhaps you should check your IDE case sensitive properties. I had the same problem, and solved it by making Idea case-sensitive (for my filesystem is case sensitive): https://confluence.jetbrains.com/display/IDEADEV/Filesystem+Case-Sensitivity+Mismatch

If you are using Intellij and the above solutions are not working try: File > Invalidate Caches / Restart

This worked for me. Sometimes when you are working through multiple branches, the IDE uses its cache and cannot find the file in the cache hence it shows error during compilation.

I got this error. To resolve this On the Project -> BuildPath -> TestNG I had to uncheck the 'Use project TestNG jar' Apply and Close. On the next run of 'mvn compile', the build was successful.