Skip to content

Using picocli with GraalVM's native-image tool #410

@stengerh

Description

@stengerh

I have tried to use the AOT compilation with the GraalVM' native-image tool. I tried something similar to the example from the picocli documentation first, i.e.

    public static void main(String[] args) {
        CommandLine.run(new Example(), System.out, args);
    }

This was unsuccessful as the compiled native executable did not seem to parse and bind the arguments to the commands fields. What did work though was to let picocli do the annotation processing in the JVM which generated the image, i.e. in a static field initializer:

    private static final CommandLine CMD = new CommandLine(new Example());

    public static void main(String[] args) {
        CMD.parseWithHandlers(new CommandLine.RunLast(),
                new CommandLine.DefaultExceptionHandler<List<Object>>(),
                args);
    }

I am posting this here so that others can benefit from it. It would be nice if you included instructions for using native-image with picocli into the documentation. :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions