So I have a really small Spring Boot command line application (There is no embedded tomcat server or anything that would make it a web application) with a single class where I try to use the Scanner
class from java.util to read input from the user. This does not work at all. I thought it would be pretty basic stuff in Spring Boot, but all of my searches both on S.O or tutorials have yielded no results. What's the best way to go about it? or is Spring Boot just not for what I want to do?
Here's my class:
package test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.Scanner;
@SpringBootApplication
public class MyApplication implements CommandLineRunner {
private static Logger LOG = LoggerFactory
.getLogger(MyApplication.class);
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MyApplication.class);
app.run(args);
}
@Override
public void run(String... args) throws Exception {
LOG.info("EXECUTING : command line runner");
Scanner in = new Scanner(System.in);
System.out.println("What is your name?");
String name = in.next();
System.out.println("Hello " + name + " welcome to spring boot" );
}
}
The exception being thrown is:
2019-05-29 11:31:29.116 INFO 4321 --- [ main] test.MyApplication : EXECUTING : command line runner
2019-05-29 11:31:29.119 INFO 4321 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-05-29 11:31:29.124 ERROR 4321 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at com.jaletechs.png.PrimeNumberGeneratorApplication.main(MyApplication.java:19) [main/:na]
Caused by: java.util.NoSuchElementException: null
at java.util.Scanner.throwFor(Scanner.java:862) ~[na:1.8.0_201]
at java.util.Scanner.next(Scanner.java:1371) ~[na:1.8.0_201]
at test.MyApplication.run(MyApplication.java:27) [main/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
... 3 common frames omitted