96

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.

mattg
  • 1,731
  • 1
  • 12
  • 20
  • Can you provide some version information (Maven in particular) etc? This isn't what happens for me. – Dave Newton Jan 04 '13 at 20:25
  • 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. – Atul S. May 30 '17 at 09:47
  • 1
    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. – Stephen C Oct 18 '20 at 03:14

17 Answers17

39

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

Ng Sek Long
  • 4,233
  • 2
  • 31
  • 38
Peter Svensson
  • 6,105
  • 1
  • 31
  • 31
  • 5
    Seems to be fixed in maven-compiler-plugin 3.1. See [george-papatheodorou](http://stackoverflow.com/users/1570793/george-papatheodorou)'s answer. – Jared Beck Oct 27 '13 at 01:45
  • 54
    This is not a solution .. the compilation error is still happening in java 8. – Eta Nov 15 '17 at 09:06
  • Started happening when Windows 10 crashed. Might that be a factor? – TheRealChx101 Aug 01 '19 at 12:27
  • 2
    To @Eta (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?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-or-cannot-resolve-symbol-error-mean). – Stephen C Oct 18 '20 at 03:04
38

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>
George Papatheodorou
  • 1,539
  • 19
  • 23
  • What is the best way to determine the latest version of this plugin? All I could find is this page: http://maven.apache.org/plugins/maven-compiler-plugin/source-repository.html Thanks. – Jared Beck Oct 27 '13 at 01:42
  • @JaredBeck seems like you can find that here: https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin – DSimon Dec 04 '19 at 23:00
11

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.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
a.parfenov
  • 528
  • 6
  • 16
  • 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. – Michael M May 10 '17 at 22:39
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
Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
AJLoveChina
  • 574
  • 6
  • 15
  • 6
    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? Thanks – turnip424 May 31 '18 at 09:54
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.

Joabe Lucena
  • 792
  • 8
  • 21
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>
Anto Livish A
  • 322
  • 4
  • 15
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.

AabinGunz
  • 12,109
  • 54
  • 146
  • 218
  • "mvn clean -DskipTests=true" install is a magic command ! Thx for sharing. (BTW I didn't touched my tests) – Eta Nov 15 '17 at 09:27
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

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.

Pragya
  • 101
  • 4
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.

Isaac
  • 16,458
  • 5
  • 57
  • 81
  • 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). – John Jun 28 '17 at 17:43
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

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.

Divya
  • 59
  • 10
-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.

asgoth
  • 35,552
  • 12
  • 89
  • 98
  • 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. – mattg Jan 04 '13 at 20:55
-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.

Stoic_Observer
  • 159
  • 1
  • 14
-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.

-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.

ulysses
  • 123
  • 1
  • 12
-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