maven "cannot find symbol" message unhelpful
Asked Answered
U

18

102

This is a really simple question, and it's probably a setting somewhere I don't know about, but Google is being particularly unhelpful for this question, giving results about compilation errors, not how to change compilation error messages.

When I build my project with maven, it will give me error messages formatted roughly as follows:

[ERROR] /path/to/source/Main.java:[13,8] error: cannot find symbol

When I build with ant or javac, it will actually tell me the symbol that it can't find in the error message. maven gives me a line number and character position, but displaying the actual symbol would be more helpful. The line above is the only line given for each of the "cannot find symbol" errors. There is no line above or below that gives the symbol. I imagine there has to be some way to get maven to tell me that information, but I don't know what it is. I tried the -e option, as mvn told me to try using it, but it gave a maven traceback for the error, not the actual symbol.

Any help?

Here's the output of 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"

And here's an example (unhelpful) error message, exactly as output by maven (just with directories shortened):

[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] -------------------------------------------------------------

The symbols it can't find are "fakeThing" and "fakeThing2", not SoundEngineFilePanel.

Unreal answered 4/1, 2013 at 20:23 Comment(3)
Can you provide some version information (Maven in particular) etc? This isn't what happens for me.Semitropical
It is true that maven “cannot find symbol” message is not very helpful. I can tell you my case where my code was using a particular method of a third party library for a long time but somehow the method was removed from the third party library. When maven was compiling the code it was being failed. If the message had been "method not found" instead of "can't find symbol" then it would have been very effective and less time consuming.Emigrate
To people who feel inclined to add yet another answer to this question. Read the question carefully. It is NOT about how to fix "cannot find symbol" compilation errors (in Maven). It is about how to get Maven to give more informative compilation error messages; i.e. messages that include the source code context ... like javac and Ant do.Brachiate
L
39

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

Lucinalucinda answered 4/1, 2013 at 21:51 Comment(4)
Seems to be fixed in maven-compiler-plugin 3.1. See george-papatheodorou's answer.Palomo
This is not a solution .. the compilation error is still happening in java 8.Zelazny
Started happening when Windows 10 crashed. Might that be a factor?Mountebank
To @Zelazny (and 27 or so others who +1'ed the above comment) - I think you are missing the point. This question is about unhelpful messages from Maven. This answer talks about how to make the compilation error messages more helpful. It does not fix the problem that caused the compilation error in the first place. That problem is (typically) in your source code! See What does a “Cannot find symbol” or “Cannot resolve symbol” error mean?.Brachiate
H
39

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>
Helico answered 1/9, 2013 at 15:58 Comment(2)
What is the best way to determine the latest version of this plugin? All I could find is this page: maven.apache.org/plugins/maven-compiler-plugin/… Thanks.Palomo
@JaredBeck seems like you can find that here: mvnrepository.com/artifact/org.apache.maven.plugins/…Chiefly
S
12

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.

Strident answered 21/9, 2016 at 15:26 Comment(1)
This was my problem as well. While there are certainly existing bugs in Maven, I would expect that in most cases, Maven errors or build issues are explained by scenarios such as these.Bost
B
6

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
Beefsteak answered 31/7, 2016 at 15:38 Comment(1)
Could you please list down the steps that you used to remove the JAR files from your build path? As I am getting the same error when I try to delete my target folder and recompile using maven? ThanksSrinagar
P
6

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.

Prothalamion answered 14/8, 2018 at 23:2 Comment(0)
M
3

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>
Marrissa answered 20/4, 2018 at 12:40 Comment(0)
A
2

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.

Ageratum answered 23/9, 2013 at 10:39 Comment(1)
"mvn clean -DskipTests=true" install is a magic command ! Thx for sharing. (BTW I didn't touched my <scope>tests</scope>)Zelazny
E
2

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

Enforcement answered 15/9, 2017 at 11:13 Comment(0)
A
1

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.

Anglesite answered 19/1, 2021 at 6:7 Comment(0)
I
0

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.

Ineffective answered 4/1, 2013 at 20:26 Comment(1)
I was getting the same error. The subsequent error gave the name of a dependency that was missing from the project (in this case an org.apache.poi.xssf library class).Ploss
B
0

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

Bilyeu answered 10/8, 2017 at 11:10 Comment(0)
A
0

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.

Attemper answered 14/8, 2021 at 15:14 Comment(0)
A
0

first do this for the entire project: mvn clean -DskipTests=true install

and then try to do clean/compile/install in the child module to fix the issue. Worked for me

Amphibian answered 20/3 at 17:5 Comment(0)
C
-1

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.

Carrageen answered 4/1, 2013 at 20:49 Comment(1)
The line that it's complaining about is fakeThing = new JPanel(). It's just a standard "cannot find symbol" error that javac would show "fakeThing" for. maven just isn't showing it.Unreal
A
-1

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.

Anear answered 29/12, 2016 at 18:58 Comment(0)
P
-1

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.

Plaid answered 11/7, 2017 at 2:33 Comment(0)
M
-1

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.

Mayotte answered 3/7, 2018 at 5:8 Comment(0)
H
-1

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

Hobo answered 12/10, 2018 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.