Why does IntelliJ give me "Package doesn't exist" error?
JavaIntellij IdeaUbuntu 12.04BarcodeJava Problem Overview
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?
Java Solutions
Solution 1 - Java
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.
Solution 2 - Java
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.
Solution 3 - Java
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!
Solution 4 - Java
None of the 13 existing answers worked for me. However, I could resolve the issue by first removing all modules:
- open
File
>Project Structure...
, - go to
Modules
tab, - select all modules and press the remove button,
then removing all remaining Maven modules from Maven
tool window:
- select all modules,
- right click on them,
- press
Remove projects
,
and then adding them again in Project
tool window:
- right click on root
pom.xml
, - press
Add as Maven project
,
now unignoring any ignored modules from Maven
tool window:
- select all ignored (grey) Maven modules,
- right click on them,
- press
Unignore
,
and finally rebuilding using Build
> Rebuild project
. This assumes that a mvn clean install
already happened.
Solution 5 - Java
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).
Solution 6 - Java
menu -> build -> Rebuild Project
has worked for me
(Invalidating caches without this step doesn't help)
Solution 7 - Java
Solution 8 - Java
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
Solution 9 - Java
Quit IntelliJ, remove every .idea
directory:
rm -Rf **/.idea/
and restart.
Solution 10 - Java
Tried all the above approaches, didn't work.
Finally running maven clean install
solved it!
Solution 11 - Java
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.
- Make sure do a success command line build (If UT failed, please ignore UT by using -Dmaven.test.skip=true.
- In the "Maven Projects" view, try to use "reimport" all the maven project.
- In the file menu, use "Invalidate Caches"
- Delete .idea folder, basically create the workspace from scratch. (That's the only reliable way can solve this issue)
Solution 12 - Java
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
)
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:
- Generate Folders and Update Sources for All Projects
- Reimport all maven projects
Solution 13 - Java
Invalidate Caches/ Restart and then Build -> Rebuild Project helped for me
Solution 14 - Java
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
Solution 15 - Java
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.
Solution 16 - Java
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)
Solution 17 - Java
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.
Solution 18 - Java
I tried
- "Maven > Reimport"
- Deleting the .idea directory, and reopening the project.
- File -> Invalidate Caches/Restart then Build -> Rebuild Project
- Deleting what is inside local .m2 folder, and downloading dependencies again.
- 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.
Solution 19 - Java
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.
Solution 20 - Java
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.
Solution 21 - Java
What happens here is the particular package is not available in the cache. Resetting will help solve the problem.
-
File -> Invalidate Caches /Restart
-
Goto terminal and build the project again
./gradlew build
This should download all the missing packages again
Solution 22 - Java
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
Solution 23 - Java
I got this error after I changed pom, and I fixed it by execute:
mvn idea:module
it works for me
Solution 24 - Java
I had the same problem. I fixed it by applying the android-apt plugin https://bitbucket.org/hvisser/android-apt
Solution 25 - Java
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)
Solution 26 - Java
If you are trying the suggested ways and still no chance, be sure about your order:
- Delete your .idea/
- Invalidate and Restart Cache afterwards
- 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.
Solution 27 - Java
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
Solution 28 - Java
In case you're facing very weird "unable to resolve java
, sun
packages problem", try the following:
- 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)
- 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.
Solution 29 - Java
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.
Solution 30 - Java
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.
Solution 31 - Java
Follow the steps below:
- Shift+Shift -> Type Invalidate Caches -> Chech all options except: Ask before...
- Superior menu -> Build -> Rebuild Project
Solution 32 - Java
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.
Solution 33 - Java
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
Solution 34 - Java
For me worked:
- Reimport
- Invalidate Caches/ Resart
- Rebuild Project