简体   繁体   English

如何修复 Eclipse 上的“声明的包与预期的包不匹配”错误?

[英]How do I fix a “declared package does not match the expected package” error on Eclipse?

I've feel like I have gone through the proper steps to fix this error, and I've looked at other questions about this in SO and other places, but for some reason I'm still having an issue.我觉得我已经采取了正确的步骤来修复此错误,并且我已经在 SO 和其他地方查看了有关此的其他问题,但出于某种原因,我仍然遇到问题。

I am trying to set up a project using the Apache JMeter source code.我正在尝试使用 Apache JMeter 源代码建立一个项目。

As an example, the file BeanShellAssertion.java is in例如,文件BeanShellAssertion.java位于

~/workspace/apache-jmeter-2.9/src/components/org/apache/jmeter/assertions.

My source folder is src/components .我的源文件夹是src/components The package declaration in BeanShellAssertion.java is BeanShellAssertion.java的包声明是

package org.apache.jmeter.assertions;

Looking at it, it seems like everything is set up the way it is supposed to be.看着它,似乎一切都按照它应该的方式进行设置。 However, the package declaration is causing an error:但是,包声明导致错误:

The declared package "org.apache.jmeter.assertions" does not match the
expected package "components.org.apache.jmeter.assertions"

Image is below.图像如下。 What exactly am I doing wrong here?我到底做错了什么?

在此处输入图片说明

在此处输入图片说明

put your src/components/ to source path把你的src/components/放到源路径

right click > properties > java build path > source > add folder > select src/components右键单击> 属性> java 构建路径> 源> 添加文件夹> 选择src/components

Once I imported an existing maven project, I right clicked on the src folder and removed it from the build path.导入现有的 maven 项目后,我右键单击 src 文件夹并将其从构建路径中删除。 This fixed the error.这修复了错误。

Removing the src folder fixes this error, but it leads to other problems.删除 src 文件夹可修复此错误,但会导致其他问题。 For example refactoring of this code does not work anymore, because it depends on the build path.例如,此代码的重构不再起作用,因为它取决于构建路径。 I recommend: Right-click the project -> Select Maven -> Update Project... -> (Set CheckBox:) Force Update... This also fixed this error for me and eg refactoring of the code in the src directories can be done afterwards.我建议:右键单击项目 -> 选择 Maven -> 更新项目... -> (Set CheckBox:) Force Update... 这也为我修复了这个错误,例如可以重构 src 目录中的代码之后完成。

在我的情况下,缺少的是 package 语句,所以我添加了(例如):

package CentralBCTestJK; // as the first line in my .java file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM