Eclipse:“声明的包与预期的包不匹配”,但是buildpath定义了声明的包。

时间:2023-01-18 10:23:33

In a maven project called my-project, eclipse keeps telling me

在一个名为my-project的maven项目中,eclipse一直告诉我。

The declared package "com.myself" does not match the expected package "main.java.com.myself"

com宣布计划”。我自己“与预期的包”main.java.com.myself不匹配。

although I verified that in the project's build path, I have an entry:

虽然我验证了在项目的构建路径中,我有一个条目:

my-project/src/main/java
    Included: **/*.java
    Excluded: (None)
    Native library location: (None)
    Ignore optional compile problems: No

What could be the problem then?

那么问题是什么呢?

What is the easiest way to fix the problem?

解决这个问题最简单的方法是什么?

I suspect that something is wrong with the eclipse settings files, because after importing the project to the workspace and converting it to a maven project, I had to change the folder structure manually in to get the conventional folder structure of a maven web project. But it could be that during those changes, something was not understood properly by eclipse.

我怀疑eclipse设置文件有问题,因为在将项目导入工作区并将其转换为maven项目后,我必须手动更改文件夹结构以获取maven web项目的常规文件夹结构。但是,在这些变化中,有些东西在eclipse中没有被正确理解。

Also, maven is able to build my project (from within eclipse, as well as from the command line).

此外,maven还能够构建我的项目(从eclipse中,以及从命令行)。

I'm just getting those compilation error from eclipse.

我只是从eclipse中获取了这些编译错误。

P.S. I've already tried project > clean, but it did not help.

P.S.我已经试过了>项目的clean,但是没有帮助。

3 个解决方案

#1


5  

Set my-project/src/main/java as the source folder (Project -> Properties -> Java Build Path -> Project -> Add Folder). Remove the old source folder.

将my-project/src/main/java作为源文件夹(Project ->属性-> java Build Path ->项目->添加文件夹)。删除旧的源文件夹。

Restart eclipse if necessary.

必要时重新启动eclipse。

Simply put, Eclipse thinks that your project begins at my-project/src. Therefore, it expects all packages to descend from there, starting with main.java.

简单地说,Eclipse认为您的项目始于my-project/src。因此,它希望所有的包都从这里开始,从main.java开始。

#2


2  

Looks like the project is still not in maven nature...

看起来这个项目还不是maven的……

Follow the steps:

遵循的步骤:

Right Click on the project folder -> Maven -> Update Project...

OR, open the command prompt, go to the root project directory, and type the following command:

或者,打开命令提示符,转到根项目目录,然后输入以下命令:

mvn eclipse:clean eclipse:eclipse

#3


0  

  1. I copied the project folder to some place outside of my eclipse workspace.
  2. 我将项目文件夹复制到eclipse工作区之外的某个地方。
  3. Then I deleted the project from the eclipse project explorer and the workspace.
  4. 然后,我从eclipse project explorer和工作区中删除了项目。
  5. Now I created a new java project with the same name, and imported the original project that I saved at (1).
  6. 现在我创建了一个具有相同名称的新java项目,并导入了我在(1)中保存的原始项目。
  7. Tah dah! no compilation errors.
  8. 发长音!没有编译错误。

#1


5  

Set my-project/src/main/java as the source folder (Project -> Properties -> Java Build Path -> Project -> Add Folder). Remove the old source folder.

将my-project/src/main/java作为源文件夹(Project ->属性-> java Build Path ->项目->添加文件夹)。删除旧的源文件夹。

Restart eclipse if necessary.

必要时重新启动eclipse。

Simply put, Eclipse thinks that your project begins at my-project/src. Therefore, it expects all packages to descend from there, starting with main.java.

简单地说,Eclipse认为您的项目始于my-project/src。因此,它希望所有的包都从这里开始,从main.java开始。

#2


2  

Looks like the project is still not in maven nature...

看起来这个项目还不是maven的……

Follow the steps:

遵循的步骤:

Right Click on the project folder -> Maven -> Update Project...

OR, open the command prompt, go to the root project directory, and type the following command:

或者,打开命令提示符,转到根项目目录,然后输入以下命令:

mvn eclipse:clean eclipse:eclipse

#3


0  

  1. I copied the project folder to some place outside of my eclipse workspace.
  2. 我将项目文件夹复制到eclipse工作区之外的某个地方。
  3. Then I deleted the project from the eclipse project explorer and the workspace.
  4. 然后,我从eclipse project explorer和工作区中删除了项目。
  5. Now I created a new java project with the same name, and imported the original project that I saved at (1).
  6. 现在我创建了一个具有相同名称的新java项目,并导入了我在(1)中保存的原始项目。
  7. Tah dah! no compilation errors.
  8. 发长音!没有编译错误。