1. Overview
1.概述
In this article, we’ll investigate the “Declared package does not match the expected package” error in a java project.
在这篇文章中,我们将研究java项目中的”声明的包与预期的包不一致“错误。
We normally expect to put our java files in folders that match the package structure. The most common cause of the error is when our IDE encounters a mismatch between the package declaration and the physical location of the java file.
我们通常期望将我们的java文件放在符合包结构的文件夹中。最常见的错误原因是我们的IDE遇到了包声明和java文件的物理位置不匹配的情况。
In this short tutorial, we’ll look at an example of this error, how it shows up in IDEs and Maven, and how to resolve it. We’ll also look at a few other tips and tricks.
在这个简短的教程中,我们将看看这个错误的例子,它是如何在IDE和Maven中显示的,以及如何解决这个问题。我们还将看看其他一些技巧和窍门。
2. Example of Error
2.错误的例子
Let’s imagine we have the following class in the src/main/java/com/baeldung/bookstore directory:
让我们设想一下,在src/main/java/com/baeldung/bookstore目录中,我们有如下的类。
package com.baeldung;
public class Book {
// fields and methods
}
We would expect this to cause an error in IDE as the package name implies the path src/main/java/com/baeldung.
我们预计这将在IDE中引起一个错误,因为包的名称意味着路径src/main/java/com/baeldung。
3. Solving the Problem
3.解决问题
It’s usually fairly straightforward to solve this problem.
解决这个问题通常是相当简单的。
3.1. Correcting Package Declaration
3.1.纠正包的声明
First, let’s make sure that the package declaration and relative source file path match. If that’s already the case, we can try to close and reopen the project again. Sometimes the IDE may be out of sync with the project on disk and needs to reimport the files, resolve dependencies and successfully recompile.
首先,让我们确保包的声明和相对源文件路径相匹配。如果已经是这样,我们可以尝试关闭并重新打开该项目。有时,IDE可能与磁盘上的项目不同步,需要重新导入文件,解决依赖关系并成功地重新编译。
Otherwise, we can correct the package declaration in the following reverse DNS format:
否则,我们可以用下面的反向DNS格式修正包的声明。
package com.baeldung.bookstore;
3.2. Correcting the Source Code’s Physical Location
3.2.纠正源代码的物理位置
It might be the case that the package is declared correctly, and the java file got mistakenly placed into the wrong directory.
可能是包的声明正确,而java文件被错误地放到了错误的目录中。
Then, we’ll move the Book class into the following correct directory location:
然后,我们将把书类移到以下正确的目录位置。
<source-path>/com/baeldung/bookstore
4. Symptoms of the Problem
4.问题的症状
Depending on our IDE of choice, the error message may appear differently. Similarly, we may see the error in maven.
根据我们选择的IDE,错误信息可能会有不同的表现。同样地,我们可能在maven中看到错误。
4.1. Error in Eclipse
4.1.在Eclipse中的错误
In Eclipse, we’ll see an error like this:
在Eclipse中,我们会看到这样的一个错误。
4.2. Error in IntelliJ
4.2 IntelliJ中的错误
In IntelliJ, we’ll get a similar error message:
在IntelliJ中,我们会得到一个类似的错误信息。
4.3. Error in Maven
4.3.Maven中的错误
Similarly, we’ll get the below error while running the maven build:
同样,在运行maven构建时,我们会得到以下错误::
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/saichakr2/baeldung-projects/tutorials/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/bookstore/Book.java:[3,8] duplicate class: com.baeldung.Book
[ERROR] /Users/saichakr2/baeldung-projects/tutorials/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/bookstore/LibraryAdmin.java:[7,12] cannot access com.baeldung.bookstore.Book
bad source file: /Users/saichakr2/baeldung-projects/tutorials/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/bookstore/Book.java
file does not contain class com.baeldung.bookstore.Book
Please remove or make sure it appears in the correct subdirectory of the sourcepath
We should note, however, the Book class will compile fine using the standalone javac command. This is because the java compiler does not require the package declaration path and relative source path to match.
然而,我们应该注意到,Book类使用独立的javac命令可以很好地编译。这是因为java编译器并不要求包的声明路径和相对源路径相匹配。
5. Error in Dependent Code
5.依赖代码中的错误
We may not spot the problem in the affected class file itself. It may show up in a class with a peer dependency:
我们可能不会在受影响的类文件本身发现问题。它可能会出现在一个有同行依赖关系的类中。
As expected, the above class could not resolve the Book class because the Book class failed to compile in the expected package.
正如预期。上述类不能解决Bookclass因为Book类未能在预期的包中进行编译。
6. Additional Tips and Tricks
6.额外的技巧和窍门
While it’s an easy fix when the file is in the wrong path, we may still encounter difficulties with a source file that appears to be in the right place in the source tree.
虽然当文件在错误的路径中时,这是一个很容易解决的问题,但我们仍然可能遇到源文件在源树中似乎处于正确位置的困难。
6.1. Verify Build Path
6.1.验证构建路径
We’ll need to verify that the build path in IDE has no errors. The default source path is mentioned as <project-name>/src/main/java and <project-name>/src/test/java. The build path should have the correct dependencies and library.
我们需要验证IDE中的构建路径是否有错误。默认的源码路径被提及为<project-name>/src/main/java和<project-name>/src/test/java。构建路径应该有正确的依赖项和库。
6.2. Additional Source Path
6.2.额外的源路径
Sometimes, it’s required to add a source folder to let maven compile those class files. Although, not recommended to do so as predefined source folders will suffice most of the time.
有时,需要添加一个源文件夹,让maven编译这些类文件。不过,不建议这样做,因为预定义的源文件夹在大多数情况下已经足够了。
Nevertheless, we can add additional sources when required using build-helper-maven-plugin:
不过,我们可以在需要时使用build-helper-maven-plugin:添加额外的源。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/<another-src></source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
7. Conclusion
7.结语
In this article, we’ve learned how a mismatch between the package declaration and the corresponding directory of the java file cause error in IDEs. We also explored a couple of ways to resolve this.
在这篇文章中,我们已经了解了包的声明和java文件的相应目录不匹配是如何在IDE中引起错误的。我们还探讨了一些解决这个问题的方法。
As always, the complete source code of the examples is available over on GitHub.
一如既往,这些示例的完整源代码可在GitHub上获得。