How to Write a non-web Application with Spring Boot

A quick guide on How to write your own Spring Boot non web application within minutes. Similar to Spring Boot web application a Spring Boot Stand Alone or non-web applications are also pretty easy.
Learn with the help of real world Spring Boot Application.

This tutorial is source code oriented so you will directly go through writing a Spring Boot non-web Application.

Maven Dependencies

If you use Maven, refer to the below pom.xml.

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="" xmlns:xsi="" xsi:schemaLocation=""> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>{spring-boot-version}></version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.amitph.springboot</groupId> <artifactId>spring-boot-command-line</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-command-line</name> <description>Demo project for Spring Boot</description> <properties> <java.version>{java.version}</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</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>
Code language: HTML, XML (xml)

Gradle Dependencies

Else, if you like working with Gradle, use the below build.gradle file.

plugins { id 'org.springframework.boot' version '{spring.boot.version}' id 'java' } apply plugin: 'io.spring.dependency-management' group = 'com.amitph.spring' version = '0.0.1-SNAPSHOT' sourceCompatibility = '{java.version}' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
Code language: Gradle (gradle)

Service Class

Let’s write a Service component of the application. Just like any web applications, a Spring Boot Stand Alone application may have a Service Layer. Note that it is annotated with @Service just like any other Spring bean.

@Service public class NonWebService { public void printMessage(String[] arguments) { System.out.println("Inside NonWebService Class. Received below arguments");; } }
Code language: Java (java)

Application Class

Finally, it is a time to write the Application. Unlike the web applications, in non-web applications we have to implement CommandLineRunner interface. Hence, we have to provide implementation for its abstract run method. This method is a main method for our application as it the starting point for the app level logic. Moreover the the run method also passes all the command line arguments.

@SpringBootApplication public class Application implements CommandLineRunner { @Autowired NonWebService service; public static void main(String[] args) {, args); } @Override public void run(String... args) throws Exception { service.printMessage(args); } }
Code language: Java (java)

Thing to Note: We have Autowired the service class just like any other Spring Autowiring.

Run the Application

In your favourite IDE you can simply right click your Application and click run. However, you can also run it from command line using application jar file. Firstly, generate your application jar file using Maven or Gradle build. After this you can run the command, pass argument and see the service prints it.

$ java -jar spring-boot-command-line.jar This is first Sprign Boot Non-Web Application . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.3.RELEASE) INFO [main] com.amitph.spring.nonweb.Application : Starting Application v0.0.1-SNAPSHOT on Amits-office-mac.local with PID 86488 (/Users/aphaltankar/Workspace/personal/spring-boot-command-line/target/spring-boot-command-line.jar started by aphaltankar in /Users/aphaltankar/Workspace/personal/spring-boot-command-line/target) INFO [main] com.amitph.spring.nonweb.Application : No active profile set, falling back to default profiles: default INFO [main] com.amitph.spring.nonweb.Application : Started Application in 0.884 seconds (JVM running for 1.278) Inside NonWebService Class. Received below arguments This is first Sprign Boot Non-Web Application
Code language: plaintext (plaintext)


In this quick, tutorial you learnt How to Create Spring Boot Non-Web Application in minutes. The application can also be called as Spring Boot Standalone application.

To develop a non web application, your Application needs to implement CommandLineRunner interface along with its run method. This run method acts like a main of your application.

The Full Source Code of the Tutorials used in this article can be found on our Github Repository.

4 thoughts on “How to Write a non-web Application with Spring Boot

  1. Thank you for writing this tutorial. I never used Spring before, so I was wondering under what circumstances I’d want to develop desktop application using Spring.

    1. Thanks for reaching out Quazi. Regarding your question, a simple answer is ‘it depends’
      First of all, Spring Doesn’t provide any framework or abstractions over the basic desktop operations. This point decides a lot.
      However, you can still use Spring for your Component initializations and wirings keeping the components physically loose coupled.

  2. Thanks for the tutorial, I was wondering how it was possible to solve the inverse situation. I have an old java executable (without spring, spring-boot) used as desktop app, and I would like to add the web layer in oder to call it with an entrypoint from another app (for example to have a start/stop)

    1. Mark, you can easily build a web application, e.g. a Spring Boot Service. For executing the executable you can use something like ProcessBuilder.
      Then, you can trigger the executable from an HTTP endpoint of your web application or from a JMS message.

Comments are closed.