31

I am trying to run a Small basic Spring boot program on my machine and I when I run clean compile (even before trying spring-boot:run) on maven I get the following Error :

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/hbenayed/eclipse-workspace/SpringBootPoc/src/main/java/com/benayed/spboot/poc/SpringBootPoc/SpringBootPocApplication.java:[3,32] package org.springframework.boot does not exist
[ERROR] /C:/Users/hbenayed/eclipse-workspace/SpringBootPoc/src/main/java/com/benayed/spboot/poc/SpringBootPoc/SpringBootPocApplication.java:[4,46] package org.springframework.boot.autoconfigure does not exist
[ERROR] /C:/Users/hbenayed/eclipse-workspace/SpringBootPoc/src/main/java/com/benayed/spboot/poc/SpringBootPoc/SpringBootPocApplication.java:[6,2] cannot find symbol
  symbol: class SpringBootApplication
[ERROR] /C:/Users/hbenayed/eclipse-workspace/SpringBootPoc/src/main/java/com/benayed/spboot/poc/SpringBootPoc/SpringBootPocApplication.java:[10,17] cannot find symbol
  symbol:   variable SpringApplication
  location: class com.benayed.spboot.poc.SpringBootPoc.SpringBootPocApplication
[INFO] 4 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.807 s
[INFO] Finished at: 2018-02-18T03:28:24+01:00
[INFO] Final Memory: 19M/167M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project SpringBootPoc: Compilation failure: Compilation failure:
[ERROR] /C:/Users/hbenayed/eclipse-workspace/SpringBootPoc/src/main/java/com/benayed/spboot/poc/SpringBootPoc/SpringBootPocApplication.java:[3,32] package org.springframework.boot does not exist
[ERROR] /C:/Users/hbenayed/eclipse-workspace/SpringBootPoc/src/main/java/com/benayed/spboot/poc/SpringBootPoc/SpringBootPocApplication.java:[4,46] package org.springframework.boot.autoconfigure does not exist
[ERROR] /C:/Users/hbenayed/eclipse-workspace/SpringBootPoc/src/main/java/com/benayed/spboot/poc/SpringBootPoc/SpringBootPocApplication.java:[6,2] cannot find symbol
[ERROR] symbol: class SpringBootApplication
[ERROR] /C:/Users/hbenayed/eclipse-workspace/SpringBootPoc/src/main/java/com/benayed/spboot/poc/SpringBootPoc/SpringBootPocApplication.java:[10,17] cannot find symbol
[ERROR] symbol:   variable SpringApplication
[ERROR] location: class com.benayed.spboot.poc.SpringBootPoc.SpringBootPocApplication
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project SpringBootPoc: Compilation failure
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.compiler.CompilationFailureException: Compilation failure
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:858)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:129)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
    ... 20 more
[ERROR] 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Here is my Pom.xml :

<?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.benayed.spboot.poc</groupId>
    <artifactId>SpringBootPoc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringBootPoc</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.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>

And here is my java Class :

package com.benayed.spboot.poc.SpringBootPoc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootPocApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootPocApplication.class, args);
    }

}

I tried many solutions I could find on internet, such as deleting the .m2 repository, adding different tags (version, fork,...) to the spring-boot-maven-plugin plugin which, I'm pretty sure, is the part causing the problem

Any suggestions ?

Vy Do
  • 46,709
  • 59
  • 215
  • 313
FrankelStein
  • 907
  • 2
  • 13
  • 30

10 Answers10

24

You need add this dependency

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

then it will work.

Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • 1
    This worked ! as well as the answer of Darren Forsythe, I have a question though: why do I need to add that dependency ? since I am including a parent in my pom, this means that all the dependencies I need are retrieved from that parent so why I have to add another dependency in order for my project to compile ? – FrankelStein Feb 18 '18 at 16:57
  • 2
    If the parent declares dependencies, which are inherited, that would be correct. However, the Spring Boot parent only declares dependencyManagement, which will only manage versions and scope, but the dependencies itself are not added in child projects. – dunni Feb 18 '18 at 20:48
11

You have 0 compile dependencies, only test-compile dependencies defined, because you've commented out spring-boot-starter-data-jpa. Spring starter's pull other dependencies in (including other starters) to create everything needed at compile time. Un-comment the spring-boot-starter-data-jpa dependency, and/or re-evaulate what dependencies you actually want.

Darren Forsythe
  • 10,712
  • 4
  • 43
  • 54
  • This worked ! as well as the answer of Do Nhu Vy, I have a question though: why do I need to add that dependency ? since I am including a parent in my pom, this means that all the dependencies I need are retrieved from that parent so why I have to add another dependency in order for my project to compile ? – FrankelStein Feb 18 '18 at 16:56
  • The parent POM is for managing dependencies, they are _all_ optional, thus you need be explicit and define them yourself. – Darren Forsythe Feb 18 '18 at 16:59
6

1) Close the project 2) Import the project again with auto-import checked

This worked for me. Maybe works for you too.

Vimukthi Sineth
  • 310
  • 2
  • 8
6

If any Wrapper folder is present inside .mvn folder of your project delete it. Also, Go to File->Settings->Build,Execution,Deployment->BuildTools->Maven->Set Maven home path as Bundled(Maven 3)

Harsha N
  • 61
  • 1
  • 1
2

Add the below section outside dependency management and do an update of project. And then the clean install of maven.

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>

And the below dependency.

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

If does not fix (Not Likely to happen) then add the other dependencies mentioned here

kemparaj565
  • 379
  • 3
  • 6
1

If you don't need web related libraries, you can add this dependency instead of spring-boot-starter-web mentioned in other answers:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
splashout
  • 537
  • 5
  • 11
0

sometime project got problem during compile or got problem. Step for the solution :

  1. check if the dependency is valid. On the topic, dependency is valid. Check the name on dependency to make sure, include the Hirarky.
  2. check your java. I tried using version 19 and it work fine then using 17 is fail!! but following example on youtube, the code work. Then the next step reload the project. Make sure your Java is up to date or newest.
  3. Reload project. This can be execute from command prompt (mvn -U idea:idea) or from your ide (on Intellij, choose your project folder -> maven -> reload project). You can try other way related to reloading your IDE. My recommendation is using command prompt. If you using IDE, try reload the project before close and open project.
  4. if nothing work.. delete .idea and then open and close your project (not recommended). When following step 3, your idea folder should be clean. Make sure your IDE not open this project before deleting the folder.
  5. use recommended IDE (related to reff link). This is just reminder.

notice: this tested on windows.

hint: sometime using lower version JDK can help. If you follow manual, try to use same version. My fault is using 19 not 17 (on youtube). The newest version have more advance than before, if you still learning following your mentor/teacher is recommended.

summary from here:

Importing to IntelliJ - Error package org.springframework.boot does not exist

0

If you have this dependency below, but still you have this error:

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

your maven still doesn't download it, for this issue may you need to vpn or check internet connection. I have the same error and I use vpn, next in "Intelij IDE" in maven window( right side) if you can not see that, you should active that, next click on "reload All Maven project" and it download it and then you can see your problem has fixed. may it helpfull.

0

I was facing this issue and nothing was working out. I tried one of the solution to change maven home path. Intelij -> Preferences -> Build, Execution, Deployment -> Build Tools -> Maven

Here I changed the Maven home path from Use Maven Wrapper to Bundled (Maven 3) and everything became fine. enter image description here

0

Had the same problem (using IntelliJ), and to solve it went through a variety of steps (not necessarily in this order), and my issue was solved:

  1. Make sure the pom.xml in your project directory has

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

    For that one can also access the Dependencies by clicking ALT + Insert

    enter image description here

    Then search for spring-book-starter-web (or a similar variation), and click Add.

    enter image description here

  2. Invalidate all Caches (File > Invalidate All Caches)

    enter image description here

    enter image description here

  3. Remove the .idea folder in your project directory

    enter image description here

  4. Uninstall every Java: JDKs and Java 8, that I had installed.

    I've used Revo Uninstaller for this, but if one is in Windows, one can also go to Settings > Apps > Apps & features (as it says here).

    Then restart the machine.

  5. Install the JDK for the Project through IntelliJ.

    For that, go to File > Project Structure > Platform Settings > SDKs.

    enter image description here

    More on this here.

  6. Update IntelliJ to a newer version (assuming it's not up to date).

    As I use JetBrains Toolbox, just add to access it and it was straightforward.

    enter image description here

    One can also update by

  7. Make sure one has MAVEN_REPOSITORY pointing to the right place.

    Eg. C:\Users\goncalo\.m2\repository.

    Confirm that in File > Settings > Appearance & Behavior > Path Variables.

    enter image description here

  8. If the Project directory has a folder called target,

    enter image description here

    Go to the Maven (in my case it is on the right-top of the IDE) and, in the Lifecycle click clean

    enter image description here

    Then try to run the project again.

Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83