Spring application does not start outside of a package

I moved the class annotated with @SpringBootApplication from the default package to a specific package and it worked.


Put your java files again to hello package.

When a class doesn’t include a package declaration it is considered to be in the “default package”. The use of the “default package” is generally discouraged, and should be avoided.

It can cause particular problems for Spring Boot applications that use @ComponentScan, @EntityScan or @SpringBootApplication annotations, since every class from every jar, will be read.

Read more here.


I had created a blank Maven folder where Java folder was the last one. I added class MainApplication.

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


@SpringBootApplication
public class MainApplication {

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

    }
}

Further, I had to create a package within Java folder such as com.test and then I moved my MainApplication class into it. Note "package com.test;" Now this default package Spring boot is looking for.

package com.test;

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

@SpringBootApplication
public class MainApplication {

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

    }
}

Then it worked fine.

Tags:

Java

Spring