Java.lang.IllegalAccessError with org.apache.commons.cli when calling the CommandLineParser parser #

I am trying to use the Apache Commons CLI library to parse command-line options in an Eclipse project, roughly following the examples in their use cases

I added the folder commons-cli-1.3.1to the folder libin the root directory of the Eclipse project.

I added this to my import:

import org.apache.commons.cli.*;

And this is at the top of mine 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 errors, but when the call starts, it parser.parsegenerates 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 do not use class loaders at the moment.

What does this error mean? How can I resolve the error and parse the arguments?

+4
3

, , .

, (1.3.1 ), .

, commons-cli-1.3.1, commons-cli-1.2 ( jarn jar )

?

  • 1.2, ( ).
  • commons-cli

? , - , . , . .

, , , 1.3.1, 1.2. 1.3.1, 1.2 , ​​.

, .

+5

commons-cli OpenPatrician. . :

Options opts = new Options();
opts.addOption(HELP_OPTION, "help", false, "Display help");
opts.addOption(OptionBuilder.withLongOpt(VERSION_OPTION)
        .withDescription("Version of this application")
        .create());
opts.addOption(FULLSCREEN_MODE, "fullscreen", false, "fullscreen mode");
opts.addOption(OptionBuilder.withArgName(WINDOWED_MODE)
        .withLongOpt("windowed")
        .hasOptionalArgs(1)
        .withArgName("widthxheight")
        .withDescription("Windowed mode with optional definition of window size like: 1280x780")
        .create());
opts.addOption(GAME_LOCALE, "lang", true, "Specify the locale to use");
opts.addOption(CLIENT_OPTION, "client", false, "Start application in client mode. Currently unused. Either client or server must be specified");
opts.addOption(SERVER_OPTION, "server", false, "Start application in server mode. Currently unused. Either client or server must be specified");

:

public void printHelp(Options options) {
    HelpFormatter formatter = new HelpFormatter();
    formatter.printHelp( "OpenPatrician", options );
}

, , :

public CommandLine parseCommandLine(Options options, String[] args) {
    try {
        // parse the command line arguments
        CommandLineParser parser = new PosixParser();
        return parser.parse( options, args );
    }
    catch( ParseException exp ) {
        printHelp(options);
        throw new IllegalArgumentException("Parsing of command line arguments failed", exp);
    }
}

, PosixPaser , . , .

0

I had the same error and I think the problem is commons-cli version 1.3.x. I switched to version 1.2 and it worked like a charm.

0
source

Source: https://habr.com/ru/post/1610133/


All Articles