How to run a Spring Boot application from a command line

A Spring Boot application can be run from a command line by running a class extending CommandLineRunner and adding the annotation@EnableAutoConfiguration.

The class also needs to extends the run method:

    @Override
    public void run(String... args) throws Exception {
	    // Put your logic here.

    } 

1- Create the following java file:

import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableJpaRepositories
@EnableAutoConfiguration
public class StaticGenerator2 implements CommandLineRunner {
    
    public static void main(String[] args) throws Exception {

        //disabled banner, don't want to see the spring logo
        SpringApplication app = new SpringApplication(StaticGenerator2.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.setWebEnvironment(false);

        // Call the run method
        app.run(args);

    }

    @Override
    public void run(String... args) throws Exception {
	    // Put your logic here.

    }
}

References:

Java 8
Spring Boot

Recent Comments