Example of a simple console Java app built with Spring Boot.
Perform a Maven installation.
From STS or similar, select "Run As..." -> "Maven install".
The .jar archive will be installed in the target
directory of the project.
Navigate to the location of the directory a run the following command:
java -jar spring-boot-console-sample-0.0.1-SNAPSHOT.jar
You should see the following output:
Hello Spring Boot from console!
First define your pom.xml
with only the right parent and the bare minimum dependencies:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
and
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
Then have your Spring Boot Application class implement the CommandLineRunner
interface, overriding its run()
method:
@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SpringBootConsoleApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
// Add your application logic here
System.out.println("Hello Spring Boot from console!");
System.exit(0);
}
}
Finally, to ensure it behaves and looks exactly like a typical command line application, we disable the splash screen, the startup logs, and set a logging level of at most WARN
in our Spring application.yml
file.
This will ensure a completely silent application startup.
spring.main:
web-application-type: none
banner-mode: 'off'
log-startup-info: false
logging.level.root: WARN