Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalAccessError using org.apache.commons.cli when calling CommandLineParser#parser

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?

like image 280
ShadSterling Avatar asked Oct 04 '15 01:10

ShadSterling


People also ask

What is the Apache Commons CLI library?

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:

What is Commons CLI in Linux?

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)

What is illegalaccesserror in Java?

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.


1 Answers

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?

  1. You can just try downgrading to 1.2 as suggested above (this helped me)
  2. Review your classpath and search for another version of commons-cli

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.

like image 128
Alexandr Priymak Avatar answered Oct 08 '22 03:10

Alexandr Priymak