我正在运行mycommand.jar,并希望它失败(其中有一个System.exit(136) )。
> java -jar mycommand.jar
> EXIT_CODE=$?
> echo $EXIT_CODE
> if [ $EXIT_CODE == 136 ]; then echo success; exit 0; else echo fail; exit 1; fi然而,由于某种原因,EXIT_CODE始终是0。
编辑:
我的程序的输出是:
08:24:51.511 [main] WARN com.aa.utils.Logger - Exiting with error code 136
java.lang.RuntimeException
at com.aa.MyCommand.run(MyCommand.java:172)
at picocli.CommandLine.executeUserObject(CommandLine.java:1939)
at picocli.CommandLine.access$1300(CommandLine.java:145)
at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2352)
at picocli.CommandLine$RunLast.handle(CommandLine.java:2346)
at picocli.CommandLine$RunLast.handle(CommandLine.java:2311)
at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2179)
at picocli.CommandLine.execute(CommandLine.java:2078)
at io.micronaut.configuration.picocli.PicocliRunner.run(PicocliRunner.java:137)
at io.micronaut.configuration.picocli.PicocliRunner.run(PicocliRunner.java:114)
at com.aa.MyCommand.main(MyCommand.java:76)
$ EXIT_CODE=$?
$ echo $EXIT_CODE
0我的Java代码:
if (mycondition) {
logger.log(LogLevel.WARN, "Exiting with error code 136");
throw new RuntimeException();
} else {
logger.log(LogLevel.WARN, "Exiting with error code 0");
System.exit(0);
}我的程序没有抛出异常的输出是:
08:24:51.511 [main] WARN com.aa.utils.Logger - Exiting with error code 136
$ EXIT_CODE=$?
$ echo $EXIT_CODE
0在这种情况下,我的Java代码是:
if (mycondition) {
logger.log(LogLevel.WARN, "Exiting with error code 136");
System.exit(136);
} else {
logger.log(LogLevel.WARN, "Exiting with error code 0");
System.exit(0);
}发布于 2022-05-24 11:31:56
我的主要方法是使用错误代码0退出,即使当命令与任何其他错误代码一起退出时也是如此。
我有这样的经历:
public static void main(String[] args) {
PicocliRunner.run(MyCommand.class, args);
}我用这个代替了它
public static void main(String[] args) {
int exitCode = PicocliRunner.execute(MyCommand.class, args);
System.exit(exitCode);
}谢谢费德里科·克雷斯·库洛卡
https://stackoverflow.com/questions/72359884
复制相似问题