"cannot find symbol" error in maven

76.1k views Asked by At

I see a lot of questions on stackoverflow on this. But still I'm not able to know what is the issue in my way of building projects.

I've two spring boot projects: we-data and we-web. we-web is dependent on we-data. we-data compiles fine on maven. But we-web gives me above error.

we-data pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.we</groupId>
    <artifactId>data</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>we-data</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

we-web pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.we</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>we-web</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.we</groupId>
            <artifactId>data</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Everytime I do maven "clean compile package install" goals on we-web I get the maven error. All the symbols that this complains isn't found are all in we-data project. For some reason, it says all the classes of we-data cannot be found in we-web. But when I run the application as spring boot app, everything is fine. No maven error matters for this.:

[INFO] ------------------------------------------------------------------------
[INFO] Building we-web 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ web ---
[INFO] Deleting C:\Users\user\Documents\GitHub\we\we-web\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 10 source files to C:\Users\user\Documents\GitHub\we\we-web\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/user/Documents/GitHub/we/we-web/src/main/java/com/we/controller/EmployeeController.java:[13,25] package com.we.service does not exist
[ERROR] /C:/Users/user/Documents/GitHub/we/we-web/src/main/java/com/we/controller/EmployeePaymentController.java:[22,9] cannot find symbol
  symbol:   class EmployeeService
  location: class com.we.controller.EmployeePaymentController
.
.
.
[INFO] 44 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.322 s
[INFO] Finished at: 2016-12-26T15:30:54+05:30
[INFO] Final Memory: 26M/253M
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "pom.xml" could not be activated because it does not exist.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project web: Compilation failure: Compilation failure:
.
.

I have 1.8 jdk being used by the STS. And also I have not put the we-data project on build path for we-web. I want this to pass, because I want to generate a war of this. Please help. I'm doing "clean compile package install" on we-data and then doing it on we-web. "Update maven project" is also not helping me.

5

There are 5 answers

2
davidxxx On

Everytime I do maven "clean compile package install" goals on we-web I get the maven error. All the symbols that this complains isn't found are all in we-data project.

1) I think you should read the introduction about Maven pom here and lifecycle in Maven.

mvn clean compile package install

is redundant for compile and package phases since the install phase includes these phases (. So, do this only :

mvn clean install

2) Your web module where you run the maven goal requires the com.we:data:0.0.1-SNAPSHOT dependency with your actual code :

    <dependency>
        <groupId>com.we</groupId>
        <artifactId>data</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

If you add or modify code and you don't install this module, the web module cannot see these changes.

So you should do mvn clean install in the com.we:data module before doing that in the web module.

Finally the simplest to avoid having non updated dependency is running mvn clean install from a multi-module project which aggregates these modules.

0
Patrikoko On

I got the same error "cannot find symbol" .

I got 2 maven modules front and back and the front module could not access to the back module despite the dependency.

The problem was on the pom.xml in the back module. I remove these lines :

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>

and I just got these lines in the front pom module.

0
mp31415 On

In my case in Spring Boot application I had the following service and accompanying non-public classes in a single ServiceFoo.java file:

@Service
public class ServiceFoo {
}
class Foo { }
class Bar { }

At some point Maven started to fail with cannot find symbol: class Service.

Only after moving classes Foo and Bar into separate java files (Foo.java and Bar.java respectively) the error disappeared. Looks like either Maven (3.5.2 and 3.6.3) or Java (1.8.0_201) bug.

4
Sal Prima On

I've been stuck on this problem too for a day and finally found the root cause and solution here what i've done:

if you have the following lines on your pom (for your case is the we-data's pom)

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>

you should remove those lines and change into:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
</plugin>

after got a clue from M. Deinum i found that he's correct about the jar compiled won't be a normal jar if you using spring-boot-maven-plugin, thanks for the clue anyway.

and also please check this question: Dependency not found in Spring Boot project

I've tried to use this:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </execution>
        </executions>
    </plugin>

it works like a charm.

0
Dinesh Sharma On

In pom.xml, Add depedency

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>

and class where you are using @Inject Import javax.inject.Inject import javax.inject.Inject;