253

I'm trying to use the barbecue barcode printing library. I have successfully added the library to IntelliJ through project structure add library. Then I imported the packages and wrote the methods, which gave me no error. The packages were available in the class.

But when I compile it gives me the error:

error: package net.sourceforge.barbecue does not exist

How can this be?

I'm coding in ubuntu, is there any other place to which I have to add the library?

Line
  • 1,529
  • 3
  • 18
  • 42
Tika
  • 2,573
  • 2
  • 12
  • 10
  • 2
    And you're certain that this JAR is in the Libraries section of your Project Structure? – Makoto Nov 22 '13 at 04:30
  • 1
    Verify that the scope of the library (in the project structure window) is `Compile`. If set to a scope of `Provided` it will cause the behavior you describe. – Javaru Nov 22 '13 at 15:47
  • If you have a dependency under a maven profile, make sure you select the correct profile in the maven tree "Profiles", when you compile the project. – Jose Rui Santos Apr 15 '21 at 16:19
  • If you are using Gradle for your project check this response : https://stackoverflow.com/a/48930705/10805602 – Valentin Jun 19 '21 at 15:37
  • This solution worked for me. Follow the given [link](https://stackoverflow.com/a/68540504/3386780) – Zia Ul Mustafa Jul 27 '21 at 07:20

38 Answers38

227

Just reimport didn't work. Following worked for me.

File -> Invalidate Caches /Restart

Then

Build -> Rebuild Project

That will reimport maven project.

Note : You need to invalidate the cache AND ALSO rebuild the project.

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
Durgesh Suthar
  • 2,914
  • 4
  • 19
  • 22
  • 15
    Worked like a charm but can you also explain why does it happen? – Raj Rajeshwar Singh Rathore Oct 29 '18 at 04:34
  • 8
    Okay, just for anyone else viewing this answer -> Note that you need to invalidate the cache AND ALSO rebuild the project. I used my intuition and did only the first part, because I do see IDEA "rebuilding" and indexing after I restart, but I don't think that is enough. You need to explicitly rebuild the project. – The-Big-K Sep 29 '20 at 23:37
  • 5
    @Raj Rajeshwar Singh Rathore it's because maven (or gradle, if you use gradle) remembers / caches parts of the previous build to then build faster each time you ask it to build your project. So when you first get the error maven remembers that this module was missing. – Boommeister Mar 01 '21 at 08:43
  • OMG, it is still helpful in 2022! – Splash Jun 17 '22 at 17:11
  • This should be accepted as answer for saving my life. – panoet Oct 26 '22 at 08:29
  • Thanks a lot, I was baffled by this for more than an hour. – umunBeing Nov 14 '22 at 07:24
  • I also removed all .iml files from project recursively, then rebuild project. And all has been ok. – Ilya Feb 25 '23 at 00:57
120

I tried to "Maven > Reimport" but the only thing that actually fixed it was to close the project, delete the .idea directory, and reopen the project.

mattalxndr
  • 9,143
  • 8
  • 56
  • 87
  • 10
    This helps but the problem appears again after some time. – hipokito Jun 19 '18 at 21:34
  • I had similar issues with IntelliJ 2018 and Gradle. It couldn't find Spring packages. Doing this fixed the issue for me. – loyalBrown Oct 23 '18 at 11:30
  • 2
    To reimport: Press Ctrl+Shift+A to find actions, and input "reimport", you will find the "Reimport All Maven Projects". – boardtc Jul 10 '19 at 15:33
  • Eureka, finally something that works! All the reimports in the world weren't fixing it. Not even "invalidate cache and restart", but this did... – rogerdpack Apr 27 '21 at 17:22
  • 1
    I think it's "Reload All Maven Projects" now. It also worked for me when invalidating cache and rebuilding didn't do anything. – Michele Palmia Jun 14 '21 at 13:17
  • If you have Maven profiles, make sure that you are enabling the right ones if needed. After enabling/disabling profiles reload all Maven projects. – Ababneh A May 17 '22 at 09:55
  • I suggest making sure that all `pom.xml` files in the project are added or recognized as Maven Projects by IntelliJ. The file icon should be a small `m`. If not, right click the `pom.xml` file, and choose `Add as a Maven Project` – Ababneh A May 24 '22 at 10:36
  • for me it was sufficient to delete the .idea/libraries folder – vorwerg-ni May 05 '23 at 15:44
63

None of the 13 existing answers worked for me. However, I could resolve the issue by first removing all modules:

  1. open File > Project Structure...,
  2. go to Modules tab,
  3. select all modules and press the remove button,

then removing all remaining Maven modules from Maven tool window:

  1. select all modules,
  2. right click on them,
  3. press Remove projects,

and then adding them again in Project tool window:

  1. right click on root pom.xml,
  2. press Add as Maven project,

now unignoring any ignored modules from Maven tool window:

  1. select all ignored (grey) Maven modules,
  2. right click on them,
  3. press Unignore,

and finally rebuilding using Build > Rebuild project. This assumes that a mvn clean install already happened.

fxnn
  • 978
  • 6
  • 19
  • 4
    Worked on IntelliJ Ultimate 2019.1.2! Invalidating Caches didn't work.Reimporting the project didn't work. This solution did! – jbx Jun 17 '19 at 08:02
  • 1
    Just tested on 2019.1.3 and yes, this is a working solution – Miki Dec 20 '19 at 09:39
  • 3
    Works for version 2019.3! – user3150947 Dec 30 '19 at 13:48
  • 1
    Works for version 2020.2 as well. Was pulling my hair out until I found this! – Mitch1077487 Aug 07 '20 at 18:18
  • 1
    Thank you! Somehow, some way, this worked for me in 2020.1.4. Shouldn't have to worry about IntelliJ breaking so much in their updates, but 2020 has been a nightmare. Thanks for sharing this fix. – Austin Brown Nov 03 '20 at 19:33
  • 2
    Worked for me after updating to 2021.1 – Marco Martins Apr 15 '21 at 11:04
  • 3
    I have no idea why this worked, but it did! Thanks! IDEA version 2022.1.2 – jmrah Jun 09 '22 at 19:27
  • 1
    This right here! IntelliJ 2021.2.3 And it worked like a charm! I should note that I was messing with this project's structure before this problem started occurring. I was added a module then removed it. – Marcos Sep 09 '22 at 18:25
  • 1
    I had a single failing module in a project with 14 modules. I deleted the problem module and re-added it and -- it worked! Thanks for the inspiration. – jomofrodo Jul 11 '23 at 19:27
52

Right click your project / Maven (at bottom) / Reimport

Edit, much later: I also saw this happen much more frequently when I had the Clover plugin installed. Drop that plugin like a bad habit!

humanity
  • 1,010
  • 9
  • 14
  • In newer versions (mine 2022.1.2) of IntelliJ, there is a Reload Project position instead of Reimport. – Mikołaj Sep 19 '22 at 14:37
45

In my case the only thing that worked is:

mvn idea:idea

The good thing is that you don't have to delete .idea folder or .iml files and loose all configuration. Everything will be preserved.

(Possibly something like gradle idea works for gradle too).

daemon_nio
  • 1,446
  • 1
  • 16
  • 19
  • 3
    Though it temporarily solved my issue and I upvoted, I later discovered that command is obsolate and can cause problems. The official page for the plugins usage states Apache Maven IDEA Plugin (RETIRED). http://maven.apache.org/plugins/maven-idea-plugin/usage.html – Onat Korucu Aug 11 '20 at 22:13
27
menu -> build -> Rebuild Project

has worked for me

(Invalidating caches without this step doesn't help)

Andrew
  • 36,676
  • 11
  • 141
  • 113
18

I did re-import all maven projects. This worked for me.enter image description here

krishna Ram
  • 639
  • 3
  • 9
  • 20
15

If you added a library to the project structure (rather than via maven, that would be different), be sure it is included as a dependency for the relevant module.

Project Structure -> Modules -> Dependencies

vikingsteve
  • 38,481
  • 23
  • 112
  • 156
11

It's a very annoying problem which happens quite often. Especially after switching to a different git branch. It wasted me way too much time to troubleshoot this kind problem. I have tried all the methods above. But I can't find a reliable way. Here I just summarize those steps which could help in my situation. Jetbrains, please fix this issue to save your customer's precious time.

  1. Make sure do a success command line build (If UT failed, please ignore UT by using -Dmaven.test.skip=true.
  2. In the "Maven Projects" view, try to use "reimport" all the maven project.
  3. In the file menu, use "Invalidate Caches"
  4. Delete .idea folder, basically create the workspace from scratch. (That's the only reliable way can solve this issue)
Jianwu Chen
  • 5,336
  • 3
  • 30
  • 35
  • 6
    Tried all of these and the only thing that worked for me was #4, closing IntelliJ, deleting the .idea folder, and reloading the project. This happened to me after creating a new git branch. – ammills01 Dec 19 '18 at 14:33
  • 3
    sounds like a bug with intellij, I wonder if there is issue created for that? – Levancho Aug 25 '19 at 23:11
  • Agree, happens to me when any git-related action is performed outside of IntelliJ. Meanwhile, it builds for me just fine, happens only when I'm doing **Build > Recompile ''** for hot swap. Hence, none of the solutions like "Open/close IntelliJ", "invalidate/rebuild project" and so on, acceptable for me. But it pisses me off seriously. – RAM237 Sep 08 '21 at 13:20
  • Deleting the .idea folder is the only way worked for me usually, but the problem is then that I lose a lot of other stuff with that as well – newhouse Nov 03 '22 at 09:05
11

In my case the problem was that there was a different repository directory configuration in IntelliJ and in settings.xml file.

Check both repository directories are the same:

IntelliJ (File > Settings > Build,Execution,Deployment > Build Tools > Maven )

IntelliJ Local Repository Configuration

settings.xml (usually in C:\Users\myuser\.m2 or /home/myuser/.m2)

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">

  ...
  <localRepository>C:\Users\myuser\.m2\repository</localRepository>
  ...
</settings>

After fixing directories run:

  1. Generate Folders and Update Sources for All Projects

enter image description here

  1. Reimport all maven projects

enter image description here

Israel Varea
  • 2,600
  • 2
  • 17
  • 24
  • Adding the localRepository line in my settings.xml fixed the issue for me. I use Intellij IDEA 2021.1 Community Edition Build#IC-211.6693.111 – Christophe Broeckx Apr 09 '21 at 05:57
10

Tried all the above approaches, didn't work. Finally running maven clean install solved it!

Pratik Nagelia
  • 281
  • 1
  • 4
  • 14
  • 1
    Crazy, but this one actually worked for me... even though org.mapstruct (1.5.1-FINAL) was everywhere in the lists, it simply wouldn't use it to build. `mvn clean install` actually worked... – Voronin Jul 02 '22 at 00:18
9

Quit IntelliJ, remove every .idea directory:

rm -Rf **/.idea/ 

and restart.

Belphegor
  • 4,456
  • 11
  • 34
  • 59
Antoine
  • 4,456
  • 4
  • 44
  • 51
  • This helped for Community Edition 2017.2. I also had to import gradle project after this. 2017 and this problem still occurs. – Kirill G. Jun 03 '18 at 10:28
  • 1
    If your project is simple JAVA project and not Maven project then Don't do this before actually looking at the list of dependencies in : File>Project Structure>Modules>Dependencies. Else you will lose the list of dependencies you would actually need. As one method to solve this issue is by downloading all the Jars listed at above location. – aumiom Sep 28 '22 at 13:13
  • I did this in addition to removing any *.iml files in the source directories to remedy the problem. It doesn't appear that IntelliJ created any new *.iml files for me. – Jason White Dec 06 '22 at 17:23
6

Invalidate Caches/ Restart and then Build -> Rebuild Project helped for me

Srikanth Reddy
  • 119
  • 1
  • 5
6

I tried all appreciated answers and none of them solve my problem!

According to Intellij community, there is a bug with Maven builds in 2020.1 and 2020.1.1 versions: https://youtrack.jetbrains.com/issue/IDEA-237320?_ga=2.235486722.203129946.1591253608-322129264.1584010541

Please try to run on 2019.3.4 version (Its worked for me from the first time)

You can download from here

https://www.jetbrains.com/idea/download/previous.html?_ga=2.190043688.203129946.1591253608-322129264.1584010541

Ahmed Salem
  • 1,687
  • 22
  • 26
  • This workaround about `path.macros.xml` file has helped me: https://youtrack.jetbrains.com/issue/IDEA-239150#focus=Comments-27-4117814.0-0 2020.1 works now. – Yaroslav Stavnichiy Jul 26 '20 at 11:46
5

Similar to cvdr. Too high a Java version can be the problem.

A collegaue just have this problem with multiple java sources in our test project. She had just updated to a later Intellij and when we investigated none of the rebuild options worked but Intellij seemed to have 'bumped' the project Java settings to Java 11 (I guess it takes highest installed version on upgrade install?) Setting it back to Java8 and the project compiles successfully.

Go File>Project Structure > Project Settings > Project and select the right ones in the drop downs.

SystemsInCode
  • 629
  • 7
  • 19
5

I got this error after I changed pom, and I fixed it by execute:

mvn idea:module

it works for me

Leif Liu
  • 61
  • 1
  • 2
4

Here is a solution worked for me: Disable the "Use --release option for cross-compilation like the following in intellij idea: got Settings -> Build,Execution,Deployment -> Compiler -> Java Compiler and disable:

Use '--release' option for cross compilation(java 9 and later)

MeirDayan
  • 620
  • 5
  • 20
3

As someone who only occasionally needs to do Java work, this was very annoying. Inevitably, packages would have been added since the last time I ran our server inside IntelliJ and it would fail to build. I found what seems to be an easier solution: just don't build within IntelliJ. Build from the command line via Maven, then make sure that the run configuration does not list Build as a "Before launch" task.

Conrad Damon
  • 711
  • 6
  • 3
3

I tried

  1. "Maven > Reimport"
  2. Deleting the .idea directory, and reopening the project.
  3. File -> Invalidate Caches/Restart then Build -> Rebuild Project
  4. Deleting what is inside local .m2 folder, and downloading dependencies again.
  5. Running mvn idea:idea in Maven console (Though this command is obsolete, I had to try.)

in different combinations.

But going from Intellij 2020 version to 2019 solved my issue.

Onat Korucu
  • 992
  • 11
  • 13
  • I tried with intellij 2019 but still issue is not resolved any other suggestions.? – Tejal Jan 02 '23 at 09:09
  • Sorry, but it has been a long time since I faced this error and the solution was a result of trial and error. So I don't have anything else than try everything listed here. – Onat Korucu Jan 09 '23 at 20:16
  • no problem thanks for replying Onat Korucu . – Tejal Jan 13 '23 at 10:43
3

I had this problem in IntelliJ 2020.3 and tried the invalidate cache option and rebuild, but the problem persisted. The only thing that fixed it was to rename the package and rename it back to what it was originally.

Timbuck
  • 233
  • 4
  • 13
2

I had the same problem and it was fixed for me by changing the "Maven home directory" in Settings from "Bundled" to my locally installed maven. Perhaps this triggered some kind of refresh somewhere since I had not changed this setting for months without any issue.

Graeme Moss
  • 7,995
  • 4
  • 29
  • 42
2

What happens here is the particular package is not available in the cache. Resetting will help solve the problem.

  1. File -> Invalidate Caches /Restart
  2. Goto terminal and build the project again

    ./gradlew build
    

This should download all the missing packages again

Andronicus
  • 25,419
  • 17
  • 47
  • 88
Kishan
  • 51
  • 1
2

If you are trying the suggested ways and still no chance, be sure about your order:

  1. Delete your .idea/
  2. Invalidate and Restart Cache afterwards
  3. Import maven projects from your maven tool

If you did not invalidate and restart cache just after deleting your .idea/, Intellij keeps generating it and that was keeping error in my case.

cmlonder
  • 2,370
  • 23
  • 35
2

I had the same issue with my unit tests. I created tests on my master branch and early everything worked well, no matter what branch I was using. But when I switched again to master branch, tests did not work anymore. What helped me was to:

close intellij -> delete .idea file from directory -> open project again by pom.xml (idk if it matters what way you open the project, but first time when I tried to open it normally by intellij, it still dit not work) -> reload all maven projects from right menu -> rebuild project and that's it, everything works now

2
  1. Delete your .idea/
  2. Invalidate and Restart Cache afterwards
  3. Re-import project
Thuan Tran
  • 351
  • 2
  • 7
1

I had the same problem. I fixed it by applying the android-apt plugin https://bitbucket.org/hvisser/android-apt

1

Maven reimport, rebuild and invalidate caches did not work. I solved it by opening a terminal and executing maven clean install in the root folder project. (IntelliJ was opened and I was able to see the IDE updating and triggering reindexation while maven was doing his job)

Dotista
  • 404
  • 3
  • 12
1

The above solutions didn't work for me. I had to add the dependency explicitly in the pom.xml.

Just add your jar under resources/library and then add it as a dependency in your pom.xml like:

        <dependency>
            <groupId>your-jar-group-id</groupId>
            <artifactId>artifact-id</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/main/resources/library/name-of-jar.jar</systemPath>
        </dependency>

Try this out if none of the above work.

Antara Datta
  • 1,909
  • 2
  • 12
  • 16
  • I am using intelliJ community edition. I have a jar file that contains class definitions from a project, that I need available in a workspace I use for "Scratch" experimental work. I included the jar file in "Project Structure" > "Modules" > "Dependencies", and that has worked for me in the past. But it keeps saying "cannot find package XYZ" as if it cant locate one of these class definitions that is referenced by an import statement. I tried everything above short of deleting the .idea folder. Your solution worked for me, much thanks. – Steve T Jan 06 '23 at 23:00
  • It turned out that the path had to be relative, because I was storing the jar file somewhere else in my file structure, not located in the project workspace: ${basedir}/../../lib-for-jars/classdefs-1.0.jar – Steve T Jan 06 '23 at 23:08
1

I solved this problem by updating my local JDK 17 to oracle JDK 17.0.7.

I can't remember which distribution I was using.

Lion
  • 965
  • 10
  • 21
0

If you do not want to destroy .idea, you can try :

  • open Project Structure > Modules
  • unmark the java folder as a source folder
  • apply / rebuild
  • then mark it again as a source folder
  • rebuild
barth
  • 431
  • 2
  • 5
0

In case you're facing very weird "unable to resolve java, sun packages problem", try the following:

  1. Open Project Structure and change Project SDK to another version, example: java 8 -> 9; 11->13, etc, and wait until it re-index all jdk's jars. Switch between jdks with same version may not work! (Ex: jetbrains jdk11 -> openjdk 11)
  2. Open a new project (or create a empty one); pause new project's indexing; close the old one; start indexing; open the old project and pause the new project's indexing and wait.
Zhwt
  • 426
  • 3
  • 13
0

I tried compile a java 8 project with JDK 12 and has the same issue. None of previous answer solved my problem.

I changed o Shortel Command Line to "JAR Manifest" and worked like a charm.

intellij

cvdr
  • 939
  • 1
  • 11
  • 18
0

Follow the steps below:

  1. Ctrl+Shift -> Type Invalidate Caches -> Chech all options except: Ask before...
  2. Superior menu -> Build -> Rebuild Project
Yair Kukielka
  • 10,686
  • 1
  • 38
  • 46
GtdDev
  • 748
  • 6
  • 14
0

I created two new classes under a new package and used it in rest of the code, Intellij was able to resolve these no red highlights were shown in the intellij editor. While building it was showing package doesn't exist error. I tried everything from gradle clean build, rebuild project, clear .idea and .gradle and reimporting the project, clearing the gradle local cache but nothing was working. Finally I refactored the name of the new package and this time intellij was able to recognize the new classes and it started working.

  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31478281) – thelearner Apr 09 '22 at 20:53
0

Invalidate caches worked for me but it's too long to recover the caches so I don't have the patience to wait for it. I found another way to solve it in my case. "Repair IDE" step 3 (delete Package search project caches) worked for me perfectly

enter image description here

jevora
  • 469
  • 1
  • 5
  • 14
0
package com.example;

import com.sun.jna.Library;
import com.sun.jna.Native;

public class C {
    public interface Kernel32 extends Library {
        public void OutputDebugStringA(String Text);
        public void DebugBreak();
        public void Sleep(int a);
    }

    public static int incint(int in1)
    {
        testpkg.xxxBird.Say("xxxee"); //only use this statement to call external lib
                                      //I don't use "import" statement.
        return in1*2;
    }
}

I am using intellij idea. You need to add testpkg path in project dependency. enter image description here

Darwin Zou
  • 26
  • 4
0

I faced the same situation in ver 2022.1.4. I tried all solutions from the first answer, nothing worked except the delete the ".idea" folder. Even through many friends mentioned this solution, I couldn't find this folder, I was looking around user profile and AppData folders. This folder was in fact created under the project folder itself.

I closed the project, and re-opened after deleting this folder.. everything back to normal. It felt like the "Intel" was lost and the IDE was only "lij" for sometime.. lol

sarath
  • 103
  • 3
  • 8
0

I tried all answers, but eventually what helped me was changing the Maven from bundled to my own Maven distribution.

I think vice versa (changing from own to bundled) will help as well.

enter image description here

xagaffar
  • 683
  • 8
  • 16