Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Commons CLI DefaultParser NoSuchMethod error

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)
like image 402
anon Avatar asked Jul 26 '18 12:07

anon


1 Answers

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:

  1. Use the same version of commons-cli that is being used by avro-tools-1.8.2.jar. I.e. commons-cli-1.2
  2. Make sure your version of commons-cli is on the classpath before avro-tools-1.8.2.jar
  3. 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.

like image 50
dpr Avatar answered Nov 09 '22 20:11

dpr