Intellij can't find classes in same package

Answered

I am using Intellij Community and am having a strange issue.

If a class depends on a class in the same package the ide can't seem to find it.  The class is highlighted in red.  If I explicitly import it instead (I shouldn't have to for classes in the same package) the red goes away.  Building the project seems to be fine and produce no errors.

Anyone seen this before?

10
47 comments

Hello Jeffrey,

Have you tried using File | Invalidate Caches and restarting IntelliJ IDEA?

I am using Intellij Community and am having a strange issue.

If a class depends on a class in the same package the ide can't seem
to find it.  The class is highlighted in red.  If I explicitly import
it instead (I shouldn't have to for classes in the same package) the
red goes away.  Building the project seems to be fine and produce no
errors.

Anyone seen this before?

---
Original message URL:
http://www.jetbrains.net/devnet/message/5258389#5258389

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


26

Yep, restarted idea, went to lunch while it reindexed, and same problem reappeared.

2
Avatar
Serge Merzliakov

I have the same issue with Intellij 9.0.2 Ultimate, Java 1.6.20 on Apple OS X 10.6.2. I have a project with 6 modules and everything was fine until one of the modules started to require importing classes in same package. I have invalidated cached repeatedly with no success. I have checked the Dependencies configuration for modules that work and the modules that exhibit this behaviour, and there is no difference.
Using an ant script to compile works fine.

1
Avatar
Serge Merzliakov

The issue occurred with one of six modules, so I deleted all but the offending module ('sick' module which cannot find classes in same package) - and the the issue was fixed. I then added the other modules back to the project one-by-one and found the the problem was related to a single module which had a reference to the 'sick' modules source path.

I found in another module this entry in .iml file

<orderEntry type="module-library">
      <library>
        <CLASSES>
          <root url="file://$MODULE_DIR$/lib/compile-no-deploy" />
        </CLASSES>
        <JAVADOC />
        <SOURCES>
          <root url="file://$MODULE_DIR$/../SICK_MODULE/src" /> <== this references the the SICK_MODULES source path
        </SOURCES>
        <jarDirectory url="file://$MODULE_DIR$/lib/compile-no-deploy" recursive="false" />
      </library>
    </orderEntry>



When I deleted this reference manually, the problem was fixed:

<orderEntry type="module-library">
      <library>
        <CLASSES>
          <root url="file://$MODULE_DIR$/lib/compile-no-deploy" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
        <jarDirectory url="file://$MODULE_DIR$/lib/compile-no-deploy" recursive="false" />
      </library>
    </orderEntry>

0

Hi Serge,

Great work!

Please try to work out steps to reproduce the problem behaviour and submit corresponding ticket with that information to IDEA tracker.

Regards, Denis

0
Avatar
Serge Merzliakov

Denis,
   Raised incident IDEA-55557. The issue can be summarized as attaching the jar file source fin 'B' module to a dependency causes the "cannot find classes in same package issue" in the module 'A' whose source is referenced.

So:
1. module B uses module A jar file (both modules in same project)
2. Open module B dependencies dialog, attach sources to module A source dir
3. open module A in IDE and open a class in any package
4. note that other classes in same package are unresolved and shown as such

Serge

2

Select Build -> Rebuild Project, it works for me!

11

when i new project from version control , the issue occurred.  i can't fix this issue using File | Invalidate Caches and rebuild project, not only that, the project shows two examples like this "XXX_example(1)". 

0

Hi, could you please provide link for the project?

0

the reason of this behaviour is two pom.xm files in a project directory. Please remove dependency-reduced-pom.xml before importing the project.

0

Invalidate the cache and restart worked for me. 

6

worked for me.Thanks a ton!!!!!!!!!!!

0

File->Invalidate Cache and Restart worked 4 me... but the issue exists for 10 years without a fix ???

2

Had exactly the same problem and answer from Dmitry Jemerov Created 


" Have you tried using File | Invalidate Caches and restarting IntelliJ IDEA? "

 

did work perfectly. Thanks

0

I am suffering from this issue and unfortunately invalid caches and restarting is not working for me.

0

Could be the https://youtrack.jetbrains.com/issue/IDEA-220511 The fix will be available in the next 2019.3.X version update.

Please try it with 2020.1 EAP version from https://confluence.jetbrains.com/display/IDEADEV/IDEA+2020.1+latest+builds

If issue remains submit a request at https://intellij-support.jetbrains.com/hc/en-us/requests/new with the sample project and IDE logs zipped (Help | Compress Logs and Show in ... action).

0

In my case (using gradle) these steps helped resolve this issue:

make sure the source files are in {project root - build.gradle folder}/src/main/java/{top package e.g. com}

0

Invalidate the cache and restart worked for me. 

Thanks
0

Is this there a ticket to fix this bug? It is ridiculous and has been around for months for me now. Yes Invalidating Caches and Restart works, but this problems happens for me at least twice a day if not more often. Then re-indexing takes minutes or a lot longer for bigger projects; what's worse is it invalidates caches for all projects not just the one so all projects must be indexed.

After the restart and indexing though it also requires me to refresh the dependencies which takes another few minutes. This is just ridiculous. Does anyone have anything more helpful or at least a sign that this is being worked on?

Thanks :)

EDIT: oh also, when it can't access a class it just can't access that class period; even from within the actual class.

1

Mark Vogel1

>Is this there a ticket to fix this bug?

See IDEA-244761, for instance. The symptoms are similar.

That said, the actual reason can be different in your case. We will be able to analyze it if you submit a new support request attaching a demo project to reproduce the issue on, and zipped log folder (Help | Collect Logs and Diagnostic Data menu option).

0

Arina Efremova 

The above ticket IDEA-244761 doesn't seem to exist or not visible for me

0

vali

Indeed, it was created as private so as to avoid log exposure.

If you have a similar issue and have tried all suggestions from this thread to no avail, please submit a new support request attaching a demo project to reproduce the issue on, and zipped log folder (Help | Collect Logs and Diagnostic Data menu option).

0

Indeed, I was looking for similar issues as I've just experienced this 'can't access class in same package or even in same class'

I am using an older version (2019.3.5) so not sure whether it'll be fixed here. Anyways, recreating the project from maven source didn't work at first but invalidating cache did.

2

The same problem with version 2020.2.3 Ultimate. Like 10 years after the first comment here. I have never had simmilar problem with Eclipse ;) 

1

Peter Varga Have you tried to invalidate caches and restart? If you have a similar problem, please follow the instructions Arina mentioned on September 30, 2020. Our support team will be glad to help you deal with that.

Thank you!

0

@Egor Klepikov yes, I tried it, and it solved the problem. But come on guys. It takes several minutes. 

1

Invalidate Caches & Restart works for me.

But as Peter said, it takes time, by a lot. The problem itself is really confusing also.

2020.2.4 Community here.

1

I ended up on this page experiencing the same problem. After having gone through a lot of hoops yesterday as I got errors like java.lang.Object expected instead of java.lang.Object, things seemed stable today after having updated JDK to 11.0.9.1 and installed a matching latest runtime using Choose Runtime plugin.

Then this evening I moved a package from xxx.model -> yyy.model (new package) and suddenly none of the classes in the package can be found from within or outside the new package!

When I copy the class to a new class (adding '2' to the name), and then refactor it back to its old name, the class becomes visible again...

Definitely some corruption in the iml files or other Jetbrains internals?

Never had such problems working with IJ for many years and it's the second day in the row that I'm losing valuable hours on IJ unreliability :-(

IJ version 2020.3.1

2

Hello,

Please report an issue on YouTrack attaching logs folder zipped ("Help | Show Log in...") after facing the issue again. We'll check it. Thanks!

0

Please sign in to leave a comment.