I'm trying to use the Apache Commons CLI library to parse command line options in an Eclipse project, roughly following the examples in their Usage Scenarios
I added the commons-cli-1.3.1
folder to the lib
folder in the root of the Eclipse project.
I added this to my imports:
import org.apache.commons.cli.*;
And this to the top of my main
:
Options options = new Options();
CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;
try {
cmd = parser.parse( options, args);
} catch ( ParseException e1 ) {
System.err.println( "Unable to parse command-line options: "+e1.getMessage() );
e1.printStackTrace();
}
It compiles without error, but when it runs the parser.parse
call generates this error:
Exception in thread "main" java.lang.IllegalAccessError: tried to access method org.apache.commons.cli.Options.getOptionGroups()Ljava/util/Collection; from class org.apache.commons.cli.DefaultParser
I am not using any class loaders at this point.
What does this error mean? How can I resolve the error and parse the arguments?
The Apache Commons CLI library provides an API for parsing command line options passed to programs. It's also able to print help messages detailing the options available for a command line tool. Commons CLI supports different types of options: A typical help message displayed by Commons CLI looks like this:
The Apache Commons CLI library provides an API for parsing command line options passed to programs. It's also able to print help messages detailing the options available for a command line tool. Commons CLI supports different types of options: POSIX like options (ie. tar -zxvf foo.tar.gz)
The IllegalAccessError extends the IncompatibleClassChangeError, which is thrown when an incompatible class change has occurred to some class definition. The IllegalAccessError exists since JDK 1.0.
This is the most probably a dependency problem.
It happens when you compile your code agains one version of the library (1.3.1 in your case) and then run with the older version of this library in your classpath.
I came across exactly this problem today when I had dependecy on commons-cli-1.3.1, but I had commons-cli-1.2 in my classpath (because I used yarn jar to launch my application)
What you should do?
What does your exception message really mean? It means that some code at runtime tries to call some method which it has no right to call. For example, this could be trying to call a private method. Usually this is caught during compilation.
But if, for example, your code tries to call some function which is public in 1.3.1, but was private in 1.2. And if you compiled agains 1.3.1 but trying to launch with 1.2 in the classpath you will get that kind of error.
Hope it is clear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With