gpt4 book ai didi

java - 使用 Spring Boot 从命令行接收输入

转载 作者:行者123 更新时间:2023-12-01 07:17:18 26 4
gpt4 key购买 nike

所以我有一个非常小的 Spring Boot 命令行应用程序(没有嵌入式 tomcat 服务器或任何使其成为 Web 应用程序的东西),其中有一个类,我尝试使用 Scanner 类从 java.util 读取用户的输入。这根本不起作用。我认为这在 Spring Boot 中是非常基本的东西,但我在 S.O 或教程上的所有搜索都没有产生任何结果。最好的方法是什么?还是 Spring Boot 不适合我想做的事情?

这是我的类(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" );
}
}

抛出的异常是:

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

最佳答案

我遵循以下方法,

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

@Component
public class MyRunner implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
System.out.println("Enter word!");
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
System.out.println(line);
}
}

它对我来说效果很好,你也可以在主类中组合命令行监听器。

关于java - 使用 Spring Boot 从命令行接收输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56358562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com