Main Tutorials

Spring Boot non-web application example

In Spring Boot, to create a non-web application, implements CommandLineRunner and override the run method, for example :


import org.springframework.boot.CommandLineRunner;

@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {

    public static void main(String[] args) throws Exception {

        SpringApplication.run(SpringBootConsoleApplication.class, args);
		
    }

    //access command line arguments
    @Override
    public void run(String... args) throws Exception {
	
        //do something
		
    }
}

1. Project Structure

A standard Maven project structure.

2. Project Dependency

Only spring-boot-starter

pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mkyong</groupId>
    <artifactId>spring-boot-simple</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>

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

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

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

    </dependencies>

    <build>
        <plugins>
            <!-- Package as an executable jar/war -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3. Spring

3.1 A service to return a message.

HelloMessageService.java

package com.mkyong.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class HelloMessageService {

    @Value("${name:unknown}")
    private String name;

    public String getMessage() {
        return getMessage(name);
    }

    public String getMessage(String name) {
        return "Hello " + name;
    }

}

application.properties

name=mkyong

3.2 CommandLineRunner example. If you run this Spring Boot, the run method will be the entry point.

SpringBootConsoleApplication.java

package com.mkyong;

import com.mkyong.service.HelloMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import static java.lang.System.exit;

@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {

    @Autowired
    private HelloMessageService helloService;

    public static void main(String[] args) throws Exception {

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

    }

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

        if (args.length > 0) {
            System.out.println(helloService.getMessage(args[0].toString()));
        } else {
            System.out.println(helloService.getMessage());
        }

        exit(0);
    }
}

4. DEMO

Package and run it.


## Go to project directory
## package it
$ mvn package

$ java -jar target/spring-boot-simple-1.0.jar
Hello mkyong

$ java -jar target/spring-boot-simple-1.0.jar "donald trump"
Hello donald trump

Download Source Code

Download – spring-boot-non-web-example.zip (5 KB)

References

  1. CommandLineRunner JavaDoc

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
12 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
asif
6 years ago

Hi, When I use spring-boot-starter-data-jpa, it stops working. Any suggestion?

Rahul
5 years ago

log written in main method is printing but log is not printing written in run(String… args) method.Kindly help to resolve.I am using spring boot log implementation.

Carol
6 years ago

Thanks

sd sd
6 years ago

Thank you!

Sriaravind
3 years ago

Is it possible to invoke the methods inside HelloMessageService class jar by an external application without calling in command line as “java -jar target/spring-boot-simple-1.0.jar “donald trump””

Anusha
2 years ago
Reply to  Sriaravind

did you find out how it can be done ?

Tet
4 years ago

When I add this jar as a dependency in another web application(non springboot) , the application server cant start.

Yadiel Pérez Villazón
5 years ago

This solution is great but how can we make it run faster, you just have to print a text and it takes about 4 seconds. Greetings and excuse my English

varun
4 years ago

The real objective of Spring Boot is not to write very basic programs like hello world. Spring Boot is an overkill for such things. The real benefit of Spring boot is when you create large web or standalone apps.

bruno
5 years ago

thank yooooouuuuuu!!!!

s d
6 years ago

Thank you for the post!
It really helped me get started with the spring boot command line app development.

Carlos
7 years ago

hello mkyong could you please do a basic tutorial for Angular Spring security single page tutorial please, you are awesome