[intellij-idea] Package name does not correspond to the file path - IntelliJ

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?

This question is related to intellij-idea packages project-structure

The answer is


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


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.


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


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.


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.


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.


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


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!


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

project structure


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.


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.


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.


Examples related to intellij-idea

IntelliJ: Error:java: error: release version 5 not supported Has been compiled by a more recent version of the Java Runtime (class file version 57.0) Error: Java: invalid target release: 11 - IntelliJ IDEA IntelliJ can't recognize JavaFX 11 with OpenJDK 11 Error: JavaFX runtime components are missing, and are required to run this application with JDK 11 ERROR Source option 1.5 is no longer supported. Use 1.6 or later Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 How to configure "Shorten command line" method for whole project in IntelliJ intellij idea - Error: java: invalid source release 1.9 Failed to resolve: com.google.android.gms:play-services in IntelliJ Idea with gradle

Examples related to packages

How to install Python packages from the tar.gz file without using pip install Package name does not correspond to the file path - IntelliJ Can I force pip to reinstall the current version? The import android.support cannot be resolved How to solve java.lang.NoClassDefFoundError? How to list all installed packages and their versions in Python? Importing packages in Java Check for installed packages before running install.packages() How do I find a list of Homebrew's installable packages? Installing a local module using npm?

Examples related to project-structure

Package name does not correspond to the file path - IntelliJ Best practice for Django project working directory structure Representing Directory & File Structure in Markdown Syntax Android Studio not showing modules in project structure Find a class somewhere inside dozens of JAR files? What is the best project structure for a Python application?