Package name does not correspond to the file path - IntelliJ

Intellij IdeaPackagesProject Structure

Intellij Idea Problem Overview


I'm trying to import a project from VCS (well, I'm doing it for the first time actually) and this is my (imported) project's structure:

The file structure

BTW. this screen is made after many tries of changing these directories' properties (in their context menus).

In these source files' I have a following error:

The error in the editor

One time it had nothing against badugi.client but it reported this error only in badugi.server. I have completely no idea how it works...

Also classes in the same directories do not see each other.

Error

This is a code from ClientWorker class which is located (as you can see in the first image) in the same directory as Server so it should know what Server is.

I'm pretty sure this code worked well in my friend's IDE. How do I configure IntelliJ to make it work?

Intellij Idea Solutions


Solution 1 - Intellij Idea

I had this same issue, and fixed it by modifying my project's .iml file:

From:

<content url="file://$MODULE_DIR$">
  <sourceFolder url="file://$MODULE_DIR$/src/wrong/entry/here" isTestSource="false" />
  <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
  <excludeFolder url="file://$MODULE_DIR$/target" />
</content>

To:

<content url="file://$MODULE_DIR$">
  <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
  <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
  <excludeFolder url="file://$MODULE_DIR$/target" />
</content>

Somehow a package folder became specified as the root source directory when this project was imported.

Solution 2 - Intellij Idea

Judging from the directory structure, you have two packages client and server, but the code expects packages badugi.client and badugi.server.

Here is a way to fix it:

  1. Position your cursor to the underlined package statement (package badugi.server)
  2. Hit ALT + ENTER
  3. Select option Move to package badugi.server. This will automatically fix your directory structure to match the declared package

or

  1. Right click src in Project explorer
  2. Select New/Package and create package badugi
  3. Select client and server packages and drag them to the badugi package

Solution 3 - Intellij Idea

I've seen this error a few times too, and I've always been able to solve it by correctly identifying the project's module settings. In IntelliJ, right-click on the top level project -> "Open Module Settings". This should open up a window with the entire project structure and content identified as "Source Folders", "Test Source Folders", etc. Make sure these are correctly set. For the "Source Folders", ensure that the folder is your src/ or src/java (or whatever your source language is), as the case may be

Solution 4 - Intellij Idea

Add this to pom.xml(inside the project tag)

<build>
  <sourceDirectory>src/main/java</sourceDirectory>
</build>

src/main/java is the source folder you want to set. If you already have this line in your pom.xml file, check if it's correct.

Solution 5 - Intellij Idea

It is possible to tell Intellij to ignore this error.

File > Settings > Editor > Inspections, then search for "Wrong package statement" (Under Java, Probable Bugs) on the right and uncheck it.

Solution 6 - Intellij Idea

You should declare in the Project structure(Ctrl+Alt+Shift+s) in the Module section mark your folders which of them are source package(blue one) and which are test ...

Solution 7 - Intellij Idea

Kotlin

For those using Kotlin who are following the docs package conventions:

> In pure Kotlin projects, the recommended directory structure is to follow the package structure with the common root package omitted (e.g. if all the code in the project is in the "org.example.kotlin" package and its subpackages, files with the "org.example.kotlin" package should be placed directly under the source root, and files in "org.example.kotlin.foo.bar" should be in the "foo/bar" subdirectory of the source root).

IntelliJ doesn't support this yet. The only thing you can do is to disable this warning, and accept that the IDE will not help you with refactorings when it comes to changing folders/files structures.

Solution 8 - Intellij Idea

I had the same issues due to corrupted or maybe outdated intellij files. Before updating to 14.0.2 I had a perfectly working project with CORRECTLY named packages and file hierarchies.

After the update, maven compilations worked without a hitch but Intellij was reporting the said error on a specific package (other packages with similar characteristics were not affected).

I didn't bother to investigate much further , but I deleted my .iml files and .idea folders, invalidated caches, restarted the IDE, and reopened the project, relying on my maven configuration.

NOTE: This, effectively deletes run and debug configurations!

Maybe someone who understands the intellij workspace files could comment on this?

Another comment for those searching into this further: Refactoring in SC managed projects can leave behind dust -- I happen to have an "old" folder which has repetitions of the current package structure. If the .iml or .idea files have any reference to these packages it's likely that intellij could get confused with references to old packages. Good luck, fellow StackExchangers.

Update: I deleted some files in a referenced maven project and the quirk has returned. So, my post is by no means a final answer.

Solution 9 - Intellij Idea

I created a package under folder src which resolved this problem.

project structure

Solution 10 - Intellij Idea

I was just fighting with similar problem. My way to solve it was to set the root source file for Intellij module to match the original project root folder. Then i needed to mark some folders as Excluded in project navigation panel (the one that should not be used in new one project, for me it was part used under Android). That's all.

Solution 11 - Intellij Idea

Maybe someone encounters a similar warning I had with a Scala project.

> Package names doesn't correspond to directories structure, this may cause problems with resolve to classes from this file Inspection for files with package statement which does not correspond to package structure.

The file was in the right location, so the helper solutions the IDE provides are not helpful[![enter image description here][1]][1] [1]: https://i.stack.imgur.com/LOXdX.png

The Move File says file already exists (which is true) and Rename Package would actually move it to the incorrect package.

The problem is that if you have Scala Objects, you have to make sure that the first object in the file has the same name as the filename, so the solution is to move the objects inside the file.

Solution 12 - Intellij Idea

I had a similar error and in my case the fix was removing the '-' character from project name. Instead of my-app, I used MyApp

Solution 13 - Intellij Idea

For me, the issue was I had refactored a package to a different name and for some reason IntelliJ had reverted that (even though the package name, in terms of git tracking, had not changed). I refactored it once more to the new name and that fixed the issue.

Solution 14 - Intellij Idea

Just Create another Package and move all folders to its ! all error will remove

Solution 15 - Intellij Idea

The simplest solution is just to rename the first line in each class.

For example

Package name does not correspond to the file path

IntelliJ tells us that the package name should be "module09.nationalratswahlen"

The current name is "oop2.module09.nationalratswahlen"

Just press Control-Shift-R and replace "oop2.module09" with "module09" in all files. However normally you should just clone a repository by File > New > Project from Version control. Then you would avoid such issues.

Solution 16 - Intellij Idea

This is tricky here. In my case, the folder structure was:

> com/appName/rateUS/models/FileName.java

The package name, which I had specified in the file FileName.java was:

package com.appName.rateUs.models;

Notice the subtle difference between the package name: it should have been rateUS instead of rateUs

Hope this helps someone!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndroid developerView Question on Stackoverflow
Solution 1 - Intellij IdeaTrannView Answer on Stackoverflow
Solution 2 - Intellij IdeaBohuslav BurghardtView Answer on Stackoverflow
Solution 3 - Intellij Ideasyd_barrettView Answer on Stackoverflow
Solution 4 - Intellij IdeaSeareneView Answer on Stackoverflow
Solution 5 - Intellij IdeaCrashthatchView Answer on Stackoverflow
Solution 6 - Intellij IdeaEmad AghayiView Answer on Stackoverflow
Solution 7 - Intellij IdeafabriciorissettoView Answer on Stackoverflow
Solution 8 - Intellij IdeablackstrypeView Answer on Stackoverflow
Solution 9 - Intellij Idea杨晨豪View Answer on Stackoverflow
Solution 10 - Intellij IdeaymkoView Answer on Stackoverflow
Solution 11 - Intellij IdeaShikkouView Answer on Stackoverflow
Solution 12 - Intellij IdeaAbdullah AkçamView Answer on Stackoverflow
Solution 13 - Intellij Ideaartin2View Answer on Stackoverflow
Solution 14 - Intellij IdeaSana EbadiView Answer on Stackoverflow
Solution 15 - Intellij IdeaRubusView Answer on Stackoverflow
Solution 16 - Intellij IdeaollehView Answer on Stackoverflow