包名与文件路径 IntelliJ 不对应

我试图从 VCS 导入一个项目(实际上,这是我第一次这么做) ,这是我的(导入的)项目结构:

The file structure

顺便说一下,这个屏幕是在多次尝试更改这些目录的属性(在它们的上下文菜单中)之后制作的。

在这些源文件中“我有一个以下错误:

The error in the editor

有一次它对 badugi.client没有任何反应,但是它只在 badugi.server中报告了这个错误。我完全不知道它是怎么工作的。

同样,同一目录中的类也不能相互看到。

Error

这是一个来自 ClientWorker类的代码,它与 Server位于同一个目录中(正如您在第一幅图中看到的) ,因此它应该知道 Server是什么。

我非常肯定这段代码在我朋友的 IDE 中工作得很好。我如何配置 IntelliJ 使它工作呢?

134873 次浏览

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

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.

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.

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.

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.

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

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

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.

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.

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 created a package under folder src which resolved this problem.

project structure

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 had a similar error and in my case the fix was removing the '-' character from project name. Instead of my-app, I used MyApp

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.

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

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.

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.