代码之家  ›  专栏  ›  技术社区  ›  Metadata

在解析Java中的命令行参数时,异常:Or.ApAC.Engul.Ccli.MistGoopOpExtExp异常

  •  1
  • Metadata  · 技术社区  · 6 年前

    我有一个JAR,它对数据库所有源系统中的所有表执行一些验证。在所有源系统中,表每四小时加载一次。有时,我希望对特定源系统的表进行验证,而不是对所有源运行JAR,因此我希望添加在运行JAR时从参数发送源系统名称的选项。到目前为止,这就是我提出的代码:

    public static void main(String[] args) throws SQLException, IOException, AddressException, InterruptedException {
        GpHiveRecords brge;
        Options options = new Options();
        Option input = new Option("s", "ssn", true, "source system names");
        input.setRequired(true);
        options.addOption(input);
        CommandLineParser parser = new DefaultParser();
        HelpFormatter formatter = new HelpFormatter();
        CommandLine cmd = null;
        try {
            cmd = parser.parse(options, args);
            if (cmd.hasOption("s")) {
                String sources = cmd.getOptionValue("s");
                if (sources == null) {
                    System.out.println("Please enter the source system names to perform the Recon..");
                } else {
                    brge = new GpHiveRecords(sources);
                    brge.createReconFile();
                }
            } else {
                brge = new GpHiveRecords();
                brge.createReconFile();
            }
        } catch (ParseException e) {
            formatter.printHelp("utility-name", options);
            e.printStackTrace();
            System.exit(1);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
    

    如果在命令中使用所需的参数,则应该是:

    java -Xdebug -Dsun.security.abc2.debug=true -Djava.security.abc2.conf=/etc/abc2.conf -Djava.security.abc2.realm=PRODEV.COM -Djava.security.abc2.kdc=ip-00-000-000-000.ab3.internal -Djavax.security.auth.useSubjectCredsOnly=false -jar /home/supusr/AutoVal/AutoVal_Dev.jar --s oracle, sap
    

    由于我没有在其他类中编写逻辑来验证从cli发送的源代码,所以在提交jar时,我已经在上面的代码中处理了绕过这个条件,在“if-else条件”中处理了是否没有“--s或--ssn”。

    if (cmd.hasOption("s")) {
                String sources = cmd.getOptionValue("s");
                if (sources == null) {
                    System.out.println("Please enter the source system names to perform the Recon..");
                } else {
                    brge = new GpHiveRecords(sources);
                    brge.createReconFile();
                }
            } else {
                brge = new GpHiveRecords();
                brge.createReconFile();
            }
    }
    

    但当我提交jar时,如下所示:

    java -Xdebug -Dsun.security.abc2.debug=true -Djava.security.abc2.conf=/etc/abc2.conf -Djava.security.abc2.realm=PRODEV.COM -Djava.security.abc2.kdc=ip-00-000-000-000.ab3.internal -Djavax.security.auth.useSubjectCredsOnly=false -jar /home/supusr/AutoVal/AutoVal_Dev.jar
    
    usage: utility-name
     -s,--ssn <arg>   source system names
    org.apache.commons.cli.MissingOptionException: Missing required option: s
            at org.apache.commons.cli.DefaultParser.checkRequiredOptions(DefaultParser.java:199)
            at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:130)
            at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:76)
            at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:60)
            at com.recordcount.entry.StartCount.main(StartCount.java:31)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:498)
            at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
    

    我从以下位置获得代码: http://commons.apache.org/cli

    有人能告诉我我在这里犯了什么错误吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   ᴇʟᴇvᴀтᴇ    6 年前

    您已设置:

    Option input = new Option("s", "ssn", true, "source system names");
    input.setRequired(true);
    

    也就是说,您告诉commons cli这个参数是 必修的 . 因此,您将收到一条“缺少必需选项”的错误消息。

    如果是可选的,请将其更改为:

    input.setRequired(false);