Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I get a Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.cli.Options.hasShortOption(Ljava/lang/String;)Z while I am trying to create an instance of DefaultParser .

My code:

    Options options = new Options();
    // option for day offset
    options.addOption(new Option("d", "day", true, "Day offset. -d 7 will 
    request for last weeks data"));
    //options.addOption("d", "day", true, "Day offset. -d 7 will request for last weeks data");
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(options, args);

Stacktrace:

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.cli.Options.hasShortOption(Ljava/lang/String;)Z
    at org.apache.commons.cli.DefaultParser.handleShortAndLongOption(DefaultParser.java:491)
    at org.apache.commons.cli.DefaultParser.handleToken(DefaultParser.java:243)
    at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:120)
    at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:76)
    at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:60)
    at domain.Main.main(Main.java:60)

Line 60 is this:

    CommandLine cmd = parser.parse(options, args);

I was just following official apache documentation - https://commons.apache.org/proper/commons-cli/usage.html.

Yes the library can be found within the jar.

Tried running a couple more combinations, which none of them really worked. On the plus side I got a different error. Kind of.

Stacktrace #2

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.cli.Options.getMatchingOptions(Ljava/lang/String;)Ljava/util/List;
    at org.apache.commons.cli.DefaultParser.handleLongOptionWithoutEqual(DefaultParser.java:404)
    at org.apache.commons.cli.DefaultParser.handleLongOption(DefaultParser.java:384)
    at org.apache.commons.cli.DefaultParser.handleToken(DefaultParser.java:239)
    at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:120)
    at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:76)
    at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:60)
    at domain.Main.main(Main.java:61)
                @ZdeněkDavid : start with mvn dependency:tree - examine its output and look for duplicate dependencies
– rkosegi
                Aug 2, 2018 at 12:05
                In your main method try to output the result of the following commands: org.apache.commons.cli.Options.class.getProtectionDomain().getCodeSource().getLocation().toURI() and org.apache.commons.cli.DefaultParser.class.getProtectionDomain().getCodeSource().getLocation().toURI(). Both of them should output the same file but are likely not in your setup. You can further investigate your classpath by taking a look at the output of System.getProperty("java.class.path"). If you're using maven or gradle mvn dependency:tree or gradle dependencies will give you more insights.
– dpr
                Aug 6, 2018 at 12:28

That is avro-tools-1.8.2.jar brings its own version of commons-cli which conflicts with the version you want to use.

According to the META-INF\DEPENDENCIES file of avro-tools-1.8.2.jar it seems to be version 1.2.

Basically you have three options:

  • Use the same version of commons-cli that is being used by avro-tools-1.8.2.jar. I.e. commons-cli-1.2
  • Make sure your version of commons-cli is on the classpath before avro-tools-1.8.2.jar
  • Re-think the need of using avro-tools and remove the dependency if it is not really needed.
  • If you really need avro-tools the first option might be the way to go. The seconds option will probably fix the issue in the first place, but might break functionality of avro-tools because of the conflicting version 1.4 of commons-cli that will be used. If you don't really need avro-tools (not talking about avro but the tools jar specifically) I'd recommend to remove the avro-tools dependency. avro-tools doesn't seem to be suited to be pulled as dependency but is more a standalone application. It bundles a bunch of external libraries that all might end up causing version conflicts on your side.

    import org.apache.commons.cli.CommandLine;
    import org.apache.commons.cli.CommandLineParser;
    import org.apache.commons.cli.Option;
    import org.apache.commons.cli.Options;
    import org.apache.commons.cli.ParseException;
    import org.apache.commons.cli.DefaultParser;
    public class Main {
        public static void main(String[] args) throws ParseException {
            Options options = new Options();
            // option for day offset
            options.addOption(new Option("d", "day", true, "Day offset. -d 7 will request for last weeks data"));
            // options.addOption("d", "day", true, "Day offset. -d 7 will request for last
            // weeks data");
            CommandLineParser parser = new DefaultParser();
            CommandLine cmd = parser.parse(options, args);
            System.out.println(cmd.getParsedOptionValue("day"));
    

    I've run it with the following args: --date 123 -d 123

    And it prints 123.

    If the error persists copy and paste the code above and check if it works for you.

    The args variable comes as a parameter from main method public static void main(String[] args). Apache CLI version is 1.4 – anon Jul 26, 2018 at 13:05 yes, i know it comes from the main method, but what args are you passing to your example? – Martín Zaragoza Jul 26, 2018 at 13:07 The options object you're creating tells that your program is expecting this: --day value or -d value – Martín Zaragoza Jul 26, 2018 at 13:12 Tried running these java -jar kafka-client-1.0-SNAPSHOT-export.jar -d=1, java -jar kafka-client-1.0-SNAPSHOT-export.jar -d 1 and this java -jar kafka-client-1.0-SNAPSHOT-export.jar -d "1" – anon Jul 26, 2018 at 13:12 I eventually gave up and just went for another solution.. Still curious why it doesn't work on my machine. – anon Jul 27, 2018 at 11:51

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.