Package name does not correspond to the file path - IntelliJ
Asked Answered
R

17

96

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?

Robinia answered 18/10, 2014 at 14:9 Comment(0)
K
74

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
Karwan answered 19/10, 2014 at 13:46 Comment(1)
What should I do if I have like, 50 classes and all are to be moved to the package? Is there a way or I just have to manually Alt+Enter them all?Mahala
D
86

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.

Doug answered 7/7, 2015 at 18:8 Comment(6)
This should be the accepted answer. You wouldn't want to go changing file structure of an application that works in production (likely causing you to have to rework the build process). Rather, you should change your IDE so that it figures it out appropriately.Copse
Wish I could upvote this more. This was the actual solution to my problem.Partnership
Thanks, Trann. Also, by the same logic you can edit it via settings using Intelij IDEA: File -> Project Structure -> Modules and mark as Sources correct path instead.Gaming
Are IML files suitable for sharing via source control? Our project has them in .gitignore. And I see an external.linked.project.id attribute on the root module element that refers to the name of the directory the project is in (which is specific to that copy). I like this solution, but only if it doesn't have to be repeated for every copy.Arvind
In my case, I have solved the issue by changing <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> into <sourceFolder url="file://$MODULE_DIR$/src/java" isTestSource="false" /> Quick and effective! Thanks for this answerStratopause
this makes me want to commit the *.iml file to source control. This is the worst.Andras
K
74

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
Karwan answered 19/10, 2014 at 13:46 Comment(1)
What should I do if I have like, 50 classes and all are to be moved to the package? Is there a way or I just have to manually Alt+Enter them all?Mahala
U
21

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

Undercurrent answered 24/2, 2016 at 22:9 Comment(1)
this saved my week. may god bless you fellow human.Vaughn
G
13

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.

Grout answered 11/8, 2016 at 9:11 Comment(3)
For my case, I changed from <sourceDirectory>src</sourceDirectory> to <sourceDirectory>src/main/java</sourceDirectory>Matthew
super1ha1@ you saved me some previous time there! Thank you.Daubery
For my point, this was the correct answer. After that you have to right click your project folder in the overview, go to your compiler section and click "Reload project".Organize
D
8

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.

Decorative answered 4/6, 2015 at 15:40 Comment(3)
I'm upvoting this one because my folder structure is correct (and gradle compiles my Java fine) and Android-Studio has the errorAiden
I upvoted this because in Scala it is allowed to have package and file path to differ. One thing that I would add is that instead of to uncheck it maybe move the flag level from error to warn or silent warn. Note that Scala and Java settings are separated.Deherrera
I find it troublesome that IntelliJ has this setting on, as an error, by default. Scala allows flatter directory structures and there are benefits for using them (if one works also in terminal, not only in the IDE).Walz
W
4

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 ...

Witham answered 15/5, 2016 at 4:25 Comment(3)
I've added it as the source package, but it keeps changing back, I don't know why.Grout
@Searene: Invalidate cache and restart may help youWitham
It's Cmd-; on Mac :)Connive
R
2

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.

Reputable answered 11/12, 2014 at 15:41 Comment(0)
L
2

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.

Langham answered 27/9, 2018 at 13:35 Comment(0)
J
1

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

project structure

Jobe answered 30/3, 2019 at 12:44 Comment(0)
M
1

This video at timestamp 2:48 helped me.

Try creating a new Java class with the IntelliJ interface whose name is com.example.packagename.YOUR_CLASS_NAME.

Then IntelliJ automatically created the package correctly for me.

Meister answered 5/7, 2022 at 15:10 Comment(0)
L
0

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.

Letourneau answered 15/4, 2015 at 12:31 Comment(0)
P
0

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 helpfulenter image description here 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.

Putrid answered 28/2, 2018 at 14:58 Comment(0)
H
0

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

Hippopotamus answered 4/1, 2021 at 18:55 Comment(0)
A
0

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.

Abscissa answered 5/4, 2021 at 16:51 Comment(0)
L
0

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

Laure answered 1/5, 2021 at 21:51 Comment(0)
B
0

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.

Babbie answered 24/5, 2021 at 17:39 Comment(0)
D
-1

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!

Diphenyl answered 13/6, 2019 at 21:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.