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)
–
–
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.
–
–
–
–
–
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.