Unable to use Apache Commons CLI Option.builder()

2019-07-07 17:44发布

问题:

In a spark shell or application (written in Scala/maven build), I am unable to use the static builder method from the Apache Commons CLI package. I have confirmed that I am including the jar in the class path and have access to the Option class along with other classes in the package like Options, DefaultParser, etc. Why can I not use this public static method in Scala?

import org.apache.commons.cli.Option

val opt = Option.builder("foo").build()

error: value builder is not a member of object org.apache.commons.cli.Option

I can however see the static fields Option.UNINITIALIZED and Option.UNLIMITED_VALUES

using commons-cli 1.3.1

Scala version: 2.11.8

Spark version: 2.2.0

command to start the shell: spark-shell --jars .m2/repository/commons-cli/commons-cli/1.3.1/commons-cli-1.3.1.jar

回答1:

Let me help you clarify your problem scenario.

You can open your .idea folder, find that it have some internal jar dependencies already, and of the list commons_cli exists, but 1.2 version.

This would lead to class collision.

The solution is straightforward, refer the doc, use the compatible constructor method.