Search code examples
spring-shell

Spring Shell - SingleItemSelector not working in Windows console but fine in Ubuntu terminal


Hello anyone is using Spring Shell?

I am trying to use SingleItemSelector which lists items and waits for the user to choose one.

It works fine but in Windows, it doesn't wait and just take null value as input even though the user doesn't do anything. I guess in the Windows console, it just gets input right after the user presses the enter button to enter a command.

@ShellMethod(key = "single", value = "Single selector")
public String singleSelector() {
    SelectorItem<String> i1 = SelectorItem.of("key1", "value1");
    SelectorItem<String> i2 = SelectorItem.of("key2", "value2");
    List<SelectorItem<String>> items = Arrays.asList(i1, i2);
    SingleItemSelector<String, SelectorItem<String>> component = new SingleItemSelector<>(getTerminal(),
            items, "testSimple", null);
    component.setResourceLoader(getResourceLoader());
    component.setTemplateExecutor(getTemplateExecutor());
    SingleItemSelector.SingleItemSelectorContext<String, SelectorItem<String>> context = component
            .run(SingleItemSelector.SingleItemSelectorContext.empty());
    String result = context.getResultItem().flatMap(si -> Optional.ofNullable(si.getItem())).get();
    return "You have selected: " + result;
}

In Linux enter image description here

In Windows enter image description here


Solution

  • The solution is to add the dependency spring-shell-starter-jna.

    This dependency is required on Windows: https://github.com/spring-projects/spring-shell/issues/1015

    <dependency>
        <groupId>org.springframework.shell</groupId>
        <artifactId>spring-shell-starter-jna</artifactId>
        <!-- <version>3.2.2</version> -->
    </dependency>