Spring boot with Shell commands


Not all applications need a fancy web user interface! Sometimes, interacting with an application using an interactive terminal is the most appropriate way to get things done.

Spring shell Author

Spring shell allows us to create custom command with the support of spring functionalities.

Following are the features of spring shell:

  • A simple, annotation driven, programming model to contribute custom commands
  • Use of Spring Boot auto-configuration functionality as the basis for a command plugin strategy
  • Tab completion, colorization, and script execution
  • Customization of command prompt, shell history file name, handling of results and errors
  • Already built-in commands, such as clear screen, gorgeous help, exit
  • ASCII art Tables, with formatting, alignment, fancy borders, etc.

Let’s start with the implementation for the same.


Add following dependency in pom.xml:

            <name>Spring Snapshots</name>

After adding this dependency, it will add support of spring shell in the project.

In this example, we’ll create some simple commands for add, multiply and subtract two integer numbers.

Following is my class for the same:

public class MathOperations {

    @ShellMethod(key = "add", value = "Add two integer value")
    public Integer add(int a, int b){
        return a+b;

    @ShellMethod(key = "subtract", value = "Subtract two integer value")
    public Integer subtract(int a, int b){
        return a-b;

    @ShellMethod(key = "multiply", value = "Multiply two integer value")
    public Integer multiply(int a, int b){
        return a*b;
  1. @ShellComponent annotation is just stereotype annotation to declare class as shell component.
  2. @ShellMethod annotation is used to define custom commands. By default, there is no need to specify the key for your command (i.e. the word(s) that should be used to invoke it in the shell). The name of the method will be used as the command key, turning camelCase names into dashed, gnu-style, names (that is, addNumbers() will become add-numbers).
  3. In key attribute, you can define multiple values which will act as different aliases for the same command.
  4. Value is a help description about the command.

So in this example, we created three commands with name add, subtract, multiply. Each command accepts two parameters of type Integer.

Now, start your project and you will see following output:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 :: Spring Boot ::        (v2.1.7.RELEASE)

2020-05-07 16:14:38.511  INFO 15507 --- [           main] t.springshell.SpringShellApplication     : Starting SpringShellApplication on decimal-ThinkPad-E460 with PID 15507 (/home/decimal/threadminions/spring-shell/target/classes started by decimal in /home/decimal/threadminions/spring-shell)
2020-05-07 16:14:38.538  INFO 15507 --- [           main] t.springshell.SpringShellApplication     : No active profile set, falling back to default profiles: default
2020-05-07 16:14:40.695  WARN 15507 --- [           main] org.jline                                : Unable to create a system terminal, creating a dumb terminal (enable debug logging for more information)
2020-05-07 16:14:40.889  INFO 15507 --- [           main] t.springshell.SpringShellApplication     : Started SpringShellApplication in 3.687 seconds (JVM running for 5.686)

After running this, it will open interactive shell.

Invoking Commands

Before invoking commands let’s see all the available commands provided by default and your project. Just execute following command:



Built-In Commands
        clear: Clear the shell screen.
        exit, quit: Exit the shell.
        help: Display help about available commands.
        history: Display or save the history of previously run commands
        script: Read and execute commands from a file.
        stacktrace: Display the full stacktrace of the last error.

Math Operations
        add: Add two integer value
        multiply: Multiply two integer value
        subtract: Subtract two integer value

Here you can see, It is showing all the custom commands which you have created. Apart from custom command, it also support some basic command in oder to interact with shell like clear, exit, quit, help, stacktrace etc.

Let’s execute commands which we created.

As each parameters accept parameters so there can be following ways to invoke commands.

shell:>add --a 10 --b 23
shell:>add 10 20
shell:>add --a 10 20
shell:>add 20 --b 30

In this, we can define parameters name with –arg value format and also we can directly pass values only as explained in above commands.

When run the project then by default it shows shell>. It can also be configured. To configure this just implement an interface with name PromptProvider and override getPrompt method as below:

public class CustomPromptProvider implements PromptProvider {

    public AttributedString getPrompt() {
        return new AttributedString("CLI-DEMO:>", AttributedStyle.BOLD.foreground(AttributedStyle.BLUE));

Here, we have mentioned CLI-DEMO as command prompt so when we run this project again, it will show as below:


Apart from that, there are mulitple options which can be configured in order to make our commands colorful.

Demo code can be found here.

To know more about it click here

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s