1
votes

I use spring boot 2 I try to create a console base application. When It start i would like to get some input of the user

@SpringBootApplication
public class ConsoleApplication implements CommandLineRunner {
    ...

    @Override
    public void run(String... args) throws Exception {

        Scanner reader = new Scanner(System.in);

        System.out.println("Enter file file path");
        this.filePath = reader.next();

        ...
    }

    public static void main(String[] args) {
        SpringApplication.run(ConsoleApplication.class, args);
    }

}

When i try to run it

java.lang.IllegalStateException: Failed to execute CommandLineRunner at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) [spring-boot-2.0.8.RELEASE.jar:2.0.8.RELEASE] at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:776) [spring-boot-2.0.8.RELEASE.jar:2.0.8.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.0.8.RELEASE.jar:2.0.8.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) [spring-boot-2.0.8.RELEASE.jar:2.0.8.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) [spring-boot-2.0.8.RELEASE.jar:2.0.8.RELEASE] at com.sita.geodb.GeoDbApplication.main(GeoDbApplication.java:175) [main/:na] Caused by: java.util.NoSuchElementException: null at java.util.Scanner.throwFor(Scanner.java:862) ~[na:1.8.0_131] at java.util.Scanner.next(Scanner.java:1371) ~[na:1.8.0_131] at com.sita.geodb.GeoDbApplication.run(GeoDbApplication.java:138) [main/:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:792) [spring-boot-2.0.8.RELEASE.jar:2.0.8.RELEASE] ... 5 common frames omitted

3
Did you ever find a way to do this? I'm stuck trying to receive input from the user but it's just not working out. - jaletechs

3 Answers

0
votes

What you provided looks correct. I created a sample that works in same manner. Must be something else you are not showing about your implementation.

import java.util.Scanner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsoleApplication implements CommandLineRunner {

   @Override
   public void run(String... args) throws Exception {
       Scanner scanner = new Scanner(System.in);

       System.out.println("What your name?");
       String name=null;
       if (scanner.hasNext()) {
            name = scanner.nextLine();
       } 
       System.out.println("Hello "+ name);  
   }

   public static void main(String[] args) {
      SpringApplication.run(ConsoleApplication.class, args);
   }
}
0
votes

Please add the following line to your build.gradle file (assuming you use gradle as a build tool):

bootRun {
    standardInput = System.in
}

This line wires the standard stdin to System.in

0
votes

Had the same problem while using Spring recently. @jaletechs Answer hinted me into the right direction.
But I had to use hasNextLine() to make it work.
So in my case:

@Service
public class SomeClass {

@PostConstruct
public void start() throws InterruptedException {
   final Scanner scanner = new Scanner(System.in);
   if (scanner.hasNextLine()) {
          String input = scanner.nextLine();
          System.out.println(input);
       }
   }
}

And then using @jaletechs setup for the build.gradle for wiring standard input to System.in:

bootRun {
   standardInput = System.in
}