简体   繁体   中英

Exception: org.apache.commons.cli.MissingOptionException while parsing command line arguments in Java

I have a jar that performs some validations on all tables in all the source systems of the database. In all the source systems, the tables load once every four hours. Sometimes I want to do the validations on tables of particular source systems instead of running the jar for all the sources and hence I want to add the option of sending source system names from the arguments while running the jar. So far this is the code I came up with:

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();
    }

If I use the required parameters in the command, this should be:

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

Since I haven't written the logic in my other class to validate for the sources sent from CLI, I have handled to bypass this condition in the above code, in the 'if-else condition' if there is no '--s or --ssn' while submitting the jar.

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();
        }
}

But when I submit the jar as below:

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)

I got the code from: http://commons.apache.org/cli

Could anyone let me know what is the mistake I am doing here ?

You've set:

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

Ie You've told Commons CLI that this parameter is required . Hence you are getting a "Missing required option" error message.

If it's optional change it to:

input.setRequired(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM