149

I have a JDK 1.7 project with a maven dependency to a local jar in my maven repo.

I'm unable to build the project using Intellij, with the errors that a symbol cannot be found (the symbol is a class importing packages from the local jar)

But I can successfully build the project using 'mvn package'.

I've spent so much time looking for solutions, things I've already done:

  • Invalidated cache
  • 'mvn clean install' the local jar dependecy
  • Marked to 'autoload snapshots' in Intellij's maven configuration
  • Reimpoted maven in Intellij
  • Syncornized

This error happend to my couple of times before, but usually just closing the Intellij and /or doing the 'mvn clean install' did the trick.

Please help.

Ram Ghadiyaram
  • 28,239
  • 13
  • 95
  • 121
Yarin Miran
  • 3,241
  • 6
  • 30
  • 27
  • 1
    Is your maven repository correctly configure with IntelliJ ? – Ruchira Gayan Ranaweera Aug 19 '13 at 14:57
  • yes it is, the path for the repository is the one that is updated when I use 'mvn install' – Yarin Miran Aug 19 '13 at 15:00
  • 3
    The local jar is a snapshot or a release version of jar ? If the jar is a snapshot, you must check 'Always update snapshots' into the intellij Maven settings to force the update for the ide. – F. Geraerts Aug 19 '13 at 15:10
  • yes it's a snapshot, I already marked that option. But still, nothing changes :( – Yarin Miran Aug 19 '13 at 15:11
  • 1
    In the Compiler part of the Settings, is the "use external build" is checked ? And in the Use external build, the "Rebuild module on dependency change" option is also checked ? – F. Geraerts Aug 19 '13 at 15:22
  • I just checked, and that options is checked! – Yarin Miran Aug 19 '13 at 15:30
  • In my case, I was relying on an external installation of Maven 2 on a project not compatible with Maven 3.When I upgraded Intellij, it switched to the internal copy, which failed. – doc Mar 30 '15 at 12:49
  • Note that this question seems to be related to [this one](https://stackoverflow.com/questions/5905896/intellij-inspection-gives-cannot-resolve-symbol-but-still-compiles-code). I don't want to say it's a duplicate, but it might interest others having the same problem. – MetaColon May 15 '19 at 16:26
  • Restarting IntelliJ worked for me. – Vishrant Oct 27 '20 at 03:36
  • Please consider rather accepting @RahulJha's answer, it's the most upvoted one and it tells how to fix it very fast and effectively. – Honza Zidek Jun 04 '21 at 23:51

32 Answers32

257

I know this is late but executing mvn idea:idea solves the issue.

The command re-generates the crucial IntelliJ IDEA files: ipr, iml, and iws, and thus re-align them with your pom.xml.

Documentation: https://maven.apache.org/plugins/maven-idea-plugin

The plugin is retired but still (as of 05.06.2021) works like magic :)

Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
Rahul Jha
  • 2,773
  • 2
  • 13
  • 14
41

Try to delete all .iml and your POM. Then copy and paste your POM again to your project and re-open and re-import it.

masoodg
  • 581
  • 5
  • 8
28

I've found out that my .iml file and pom were conflicting and causing the issue.

Paulo Merson
  • 13,270
  • 8
  • 79
  • 72
Yarin Miran
  • 3,241
  • 6
  • 30
  • 27
  • 1
    I have similar problem. I have configuration with build date plugin. This plugin requires buildDate property with this value "${maven.build.timestamp}". IntelliJ has problem with parsing this pom and iml file isn't properly synchronised with pom. When I comment property buildDate synchronisation works fine. – chalimartines Aug 20 '13 at 08:53
  • 12
    any tips on how to fix this? I'd just like to Intellij to work, it seems Netbeans runs any project no matter what... this is a complete productivity destroyer :( ... – Breno Salgado Oct 09 '13 at 03:45
  • 1
    I don't know if your fix was the same but I had to quit and reload the project to force IntelliJ to update it's IML file (it hadn't refreshed it from pom.xml as I'd added some new dependencies) – SonarJetLens Apr 30 '14 at 14:43
  • 3
    The 'how' is quite more useful than the 'why'. Please add some details on how you fixed this issue. – João Matos Sep 12 '17 at 12:41
  • 3
    I closed IntelliJ. Then, I deleted `C:\Users\USERNAME\.IntelliJIdea2017.2\system`. Afterwards, I did a `git clean -xdf` in the source directory to remove all non-versioned files, and imported the project again. Works like a charm now. – koppor Oct 18 '17 at 06:32
  • 1
    Just delete the .iml file of the affected module the and then right click pom.xml, under maven options select unignore project/module to re create .iml file for that module – Bikas Katwal Aug 14 '18 at 20:41
  • Worked for me, deleted `.idea` and `.i*` files and reimported the project and it was fine. – craastad Mar 27 '19 at 18:43
  • YarinMiran Please consider rather accepting @RahulJha's answer, it's the most upvoted and it tells how to fix it very fast and effectively. – Honza Zidek Jun 04 '21 at 23:50
25

Maven ReImport worked for me like below...

enter image description here

Ram Ghadiyaram
  • 28,239
  • 13
  • 95
  • 121
21

Adding some more tips since this is the first thing that came up in the search when I was having similar problems (project builds in mvn, not in IntelliJ), in case it might help someone else.

When I had this it was a different fix on Windows and Mac.

On Mac, I went into ItellijIDEA / Preferences / Build, Execution, Deployment / Build Tools / Maven / Maven Home Directory and set it to use my separately installed Maven rather than the bundled Maven. All the build problems went away.

On Windows, I was seeing exceptions in the idea.log related to SSL and my project tree had only Java under the External Libraries branch. From looking at other sites it sounds like IntelliJ uses its own private JDK instead of the system one (even though in my case I already had the exact same JDK version installed). My system JDK has certs installed in the keystore for the corporate Nexus server that the IntelliJ private JDK didn't have, so exceptions were being thrown when it tried to get files from Nexus. For this the solution was to quit IntelliJ, set an environment variable called IDEA_JDK to point to the JDK I already had installed that had the proper certs and restart IntelliJ. All the build problems went away.

rrjp
  • 321
  • 2
  • 3
  • same issue on Linux with latest IntelliJ 2016.3.5 - first it worked, then after some POM changes it got ouf of sync, and only switching the Maven impl helped, so it could also be that just switching Maven triggers an update in IntelliJ that causes the project to be in sync again... – Gregor Mar 15 '17 at 14:53
  • You are good. This solve my issue. thanks – c.sankhala Feb 04 '21 at 09:17
14

IDE uses its own bundled copy of Maven when doing builds. If maven on terminal is different from IDE, then you want them to be the same.

GOTO (On a Mac)- IntelliJ Idea -> Preferences -> Build,Execution,Deployment -> Build Tools -> Maven. Set the path for your Maven Home Directory To find the path for Maven which you use from terminal, type command mvn -version and that should print out bunch of details including the dir path. Put that in Maven Home Directory.

To be safe you can also do the same for JDK location

==== Some Other Things that can cause such issues ===

If doing mvn package or mvn clean install builds, then it has to do something with IDE caches. Among other things, this one usually helps with such errors -->

File -> Invalidate Caches

ARK
  • 3,734
  • 3
  • 26
  • 29
6

I know I am pretty late to this conversation, but just wanted to post this in case someone else comes across this problem. Another reason why this happens is because you are not referring to the right repository. Intellij picks up the bundled version of a jar by default instead of the one from your own maven repository. Confirm that the repository connected is the right one.

Optimiser
  • 110
  • 3
  • 5
  • If you have installed maven using homebrew, setting maven home directory correctly (for example) /usr/local/Cellar/maven/3.5.4/libexec resolves this issue – sushilshimpi Oct 09 '18 at 17:46
4

from command prompt execute the commnad : mvn idea:idea and right click on the project -> from the menu re-import the dependencies

ravibagul91
  • 20,072
  • 5
  • 36
  • 59
4

I had a similar problem, where below options din't work.

  1. invalidating cache and restart through File menu
  2. mvn idea:idea

What resolved the issue:

  1. Close IntelliJ completely
  2. From terminal, execute git clean -xdf on the repository. This removes all generated classes and idea/iml/ipr files. Other VCS should have similar macros.
  3. Run mvn clean package
  4. Open IntelliJ with idea .

Key thing here was to close intellij, and do these steps from terminal. Somewhere IntelliJ was still caching old files if trying from within IntelliJ. This is with version IntelliJ IDEA 2021.3.2 (Ultimate Edition).

sgX
  • 696
  • 1
  • 8
  • 16
3

In my case my iml was not in sync, so I deleted the iml for the specific module and then under maven option select unignore project to recreate iml

Bikas Katwal
  • 1,895
  • 1
  • 21
  • 42
3

This simple procedure takes about 1 minute without any risc.

  • Close IntelliJ
  • Delete ONLY the IntelliJ specific files like .idea/*, *.iml, .settings, etc. Suggested was deleting the POM as well, but that is not necessary IMHO.
  • Then re-create the project in IntelliJ using: File > New > Project from existing Sources > follow the steps. Be aware NOT to import multiple profiles or (same projects).
tm1701
  • 7,307
  • 17
  • 79
  • 168
1

"symbol cannot be found" means that intellij is not able to resolve the "dependency". So, you can force "Dependency analysis" by clicking "Analyze" -> "Analyze Dependencies" then select the "Analysis Scope"

dgm
  • 2,287
  • 1
  • 23
  • 27
1

Many valuable tips in the other answers here, just want to add the solution that worked for my problem.

We recently started using a private Maven repository and always do mvn -s settings.xml when invoking the Maven cmdline. IntelliJ does of course not know about the private repo, so it needs a little help.

Go to Settings -> Build, Execution, Deployment -> Build tools -> Maven and override the "User Settings file" option.

enter image description here

Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172
1

If you are using gradle , try below command.This resolved my issue

gradle cleanIdea idea

Liju John
  • 1,749
  • 16
  • 19
1

I had the same problem. Go to:

File -> Project Structure -> Module. Select your module and under "Dependencies" tab, make sure that all the maven dependencies are added.

To make sure you are warned about a similar issue in the idea editor: switch from "Dependencies tab" to "Sources" and add mark your src as source.

Denorm
  • 466
  • 4
  • 13
1

For me the problem was that I had Ajc compiler selected in the IntelliJ Java Compiler settings. Once I changed it back to Javac everything worked fine.

Redar
  • 83
  • 10
1

I had the similar problem and I realized my maven dependencies was not updated in intellij. The solution was to Reload All Maven Projects in maven window which force intellij to reload maven dependencies.enter image description here

Marko
  • 101
  • 6
0

I had the same problem. I've changed maven version from 3.0.5 to 3.0.4 in Idea settings and all works fine now. Don't know why that helps me.

Boris Mitioglov
  • 1,092
  • 4
  • 16
  • 32
0

I tried invalidating cache, changing the Maven to installed version and nothing worked.

Closing and recreating a new project resolved it.

Anubha
  • 1
  • 1
0

I closed the project and imported it again by clicking on the root pom (not just the folder, but the pom file itself).

Togepi
  • 63
  • 1
  • 4
0

I tried everything above with no joy. However I did one thing that finally got it to work:

I went to C:\Users\.IntellijJIdea2018.2 and just deleted the entire folder.

It removes all the settings and cache for IntelliJ. I had to reimport my project from scratch but a simple remove and reimport hadn't worked before, but this finally got it working again.

Emma
  • 1
0

Delete the .idea folder and close the IDE

open the IDE again and import the project which will do a fresh import because .IDEA folder is deleted by us earlier

Check the external libraries module and if it is not populated with the required dependencies then check if the maven settings.xml location is given correctly in the IDE

firstpostcommenter
  • 2,328
  • 4
  • 30
  • 59
0

There is also a possibility which was not mentioned. If you import library from com.sun.*. It will also cause this problem.

Programs works when run in ide, cause these classes in com.sun.* are all in jre/lib directory, it will be loaded when running. But when you run mvn package, these libraries won't be accessible because of the restriction of oracle since java 1.6(these libraries was deprecated in a way). To solve this problem, you can add

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <compilerArguments>
                        <verbose />
                        <bootclasspath>C:/Program Files/Java/jdk1.8.0_161/jre/lib/rt.jar</bootclasspath>
                    </compilerArguments>
                </configuration>
            </plugin>
        </plugins>
    </build>

remember to replace the cootclasspath with you own path of rt.jar

You can search Access restriction: The type XXX is not accessible due to restriction on required library for more detail.

nilknow
  • 319
  • 3
  • 6
0

If there are erroneous modules configured them do these steps:

  • Go to Project Settings
  • Go to Modules
  • Delete modules that should not be there
OSGI Java
  • 545
  • 7
  • 21
0
  1. From maven tab click + and choose pom.xml
  2. From maven tab click download sources and documentation
  3. On project structure(where you can view project files/directories) right click the project you're trying to build and choose Build Module Project Name.
  4. From tab Run- Edit Configurations with + add Application and fill the below fields: i. Main Class- Manually choose from Project tab select the main class ii. Use classpath of module - choose the application name iii. Shorten command line - classpath file
  5. Now simply run the app.
0

In my case the problem was in different Project Java SDK version. When I was building project from terminal, maven used Java 8, and when I was building from IDEA, it used Java 11.

Go to File > Project Structure... and in Project Settings set Project SDK to the version you need.

0

For me in Mac, I solved it by deleting the Intellij Idea Appication from my computer and deleting the cache files in ~/Library/Application Support/JetBrains/IntelliJIdea2020.1 ~/Library/Caches/JetBrains/IntelliJIdea2020.1

Then re-install it, open my project, and build it successfully!

Xi Zhao
  • 1
  • 1
0

I encountered the same problem. I can successfully run the mvn clean package command in terminal but fail to run or build from IntellJ. I did the following actions but they all failed.

  • Invalidated cache
  • run mvn clean package or mvn clean package in terminal
  • Marked to 'autoload snapshots' in Intellij's maven configuration
  • Syncornize or Reimport
  • Delete all the IntellJ related files like .idea and reimport project
  • remove all the dependency files and then run mvn clean package
  • Switch Java version or use embedded Java in IntellJ
  • run mvn idea:idea
  • Change Maven Home
  • mark src directory as source in the module tab

The last thing I did worked: Delete the .idea and .iml in the project, delete all the dependency files and then delete the IntellJ from the computer. After reinstalled the IntellJ my project built successfully. I wish I could have a better way to solve this.

Adam
  • 17
  • 1
0

Another reason for this error is usage of dependencies such as lombok in pom.xml file. If you are using lombok annotations such as @Data in the java file, then you need to install lombok plugin in intellij to get rid of "Errors" in IDE.

Lokesh Garg
  • 71
  • 1
  • 1
  • 5
0

I also was getting the same problem. From terminal using maven command (mvn clean install -DskipTests=true) project was built successfully. But in intellij it was unable to identify the jar spring library files.

Fixed it using the following steps:

  1. close your IDE(intellij)
  2. delete sudo "rm -rf /<your_project_location>/.idea/" (/home/user/projects/test_proj/.idea/)
  3. open your IDE again. It will reindex your files and issue will be resolved

RCA: I suspect it happens due to permission issue in linux

0

Such problem occurs when there is a mismatch between your system JVM/Maven configuration and JVM/Maven configuration of your IntelliJ IDE. I was facing the similar issue. I was successfully able to rum "mvn clean install" command but IntelliJ Build was failing.

I also tried with "mvn idea:idea" command but it did not work because "idea" plugin is no longer exist. It has been retired.

Finally, I found a mismatch between my MAVEN_HOME path and IntelliJ default maven settings. So, my problem got fixed after correcting the IntelliJ default maven settings.

You can find the settings on following location. Go to File -> Settings -> Build, Execution, Deployment in your IntelliJ ide.

enter image description here

Make sure that the path of "User Settings File" and "Local Repository" is same as the path of environment variable "MAVEN_HOME" or "M2_HOME".

Gunjan Shah
  • 5,088
  • 16
  • 53
  • 72
0

This seems to be issue with intellij in identifying your maven jars from local .Go to File->Project Structure->Libraries And check if any maven library showing in red (means its not identified by build )

Now wherever you see in red remove them using below minutes (-) icon enter image description here

Now add them manually from your system using below plus(+) icon enter image description here

Build now and it should work .

Codified
  • 135
  • 9