我正在使用 spring-shell 开发 CLI 应用程序。
@SpringBootApplication
@CommandScan
public class App {
public static void main(String[] args) throws Exception {
SpringApplication application = new SpringApplication(App.class);
application.setBannerMode(Mode.OFF);
application.run(args);
}
它运行良好,直到我遇到这个问题:当我尝试运行我的应用程序时:
java -jar my.jar com.mycompany.App --flag
我收到以下异常:
Caused by: org.springframework.shell.CommandNotFound: No command found for '--flag'
at org.springframework.shell.Shell.evaluate(Shell.java:207) ~[spring-shell-core-3.1.5.jar:3.1.5]
at org.springframework.shell.Shell.run(Shell.java:159) ~[spring-shell-core-3.1.5.jar:3.1.5]
at org.springframework.shell.jline.NonInteractiveShellRunner.run(NonInteractiveShellRunner.java:146) ~[spring-shell-core-3.1.5.jar:3.1.5]
at org.springframework.shell.DefaultShellApplicationRunner.run(DefaultShellApplicationRunner.java:65) ~[spring-shell-core-3.1.5.jar:3.1.5]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:762) ~[spring-boot-3.1.5.jar:3.1.5]
确实没有
--flag
的命令。这意味着是一个启动选项,而不是一个应该由 shell 解释的命令。 (问题#1)。
另一个问题是,只要我提供一些命令行参数,spring-shell 就会在
NonInteractiveShellRunner
中运行。我希望它以交互模式运行。 (问题#2)。
我怎么能这么说呢?我阅读了文档,但找不到这个难题的解决方案。
据我所知,spring-shell 中没有选项告诉它不要解析命令。至少不是开箱即用的。然而,您可以做的是解析源代码
String[] args
并创建一个不包含--flag
参数的新参数实例(myArgs),并使用修改后的参数启动应用程序application.run(myArgs)
当我尝试使用 spring boot CLI 参数运行应用程序时,我遇到了非常类似的问题:
java -jar myapp.jar --spring.config.location=myapp.properties
并得到了No command found for '--spring.config.location=myapp.properties'
。
所以我将命令行更改为
java -Dspring.config.location=myapp.properties -jar myapp.jar
。
它可能对您的情况也很有用。