Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this article, we'll investigate the “Declared package does not match the expected package” error in a java project.

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.

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.

2. Example of Error

Let's imagine we have the following class in the src/main/java/com/baeldung/bookstore directory:

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.

3. Solving the Problem

It's usually fairly straightforward to solve this problem.

3.1. Correcting Package Declaration

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.

Otherwise, we can correct the package declaration in the following reverse DNS format:

package com.baeldung.bookstore;

3.2. Correcting the Source Code's Physical Location

It might be the case that the package is declared correctly, and the java file got mistakenly placed into the wrong directory.

Then, we'll move the Book class into the following correct directory location:

<source-path>/com/baeldung/bookstore

4. Symptoms of the Problem

Depending on our IDE of choice, the error message may appear differently. Similarly, we may see the error in maven.

4.1. Error in Eclipse

In Eclipse, we'll see an error like this:

 

4.2. Error in IntelliJ

In IntelliJ, we'll get a similar error message:

4.3. Error in Maven

Similarly, we'll get the below error while running the maven build:

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

5. Error in Dependent code

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.

6. Additional Tips and Tricks

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

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.

6.2. Additional Source Path

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.

Nevertheless, we can add additional sources when required using 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

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.

As always, the complete source code of the examples is available over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!