What do I do if PB library conflicts occur when I use Tablestore SDK for Java?

更新时间:
复制 MD 格式

This conflict occurs when protobuf-java or httpasyncclient versions bundled by Tablestore SDK for Java clash with versions pulled in by other dependencies in your Maven project. Use the jar-with-dependencies classifier to resolve it.

Note

To diagnose the conflict, run mvn dependency:tree in your repository and look for multiple versions of protobuf-java, httpasyncclient, or tablestore in the output.

Problem description

The following exception occurs when using Tablestore SDK for Java:

Caused by: java.lang.UnsupportedOperationException: This is supposed to be overridden by subclassed
        at com.google.protobuf.GeneratedMessage.getUnknownFields(GeneratedMessage.java:225) 

Cause

Tablestore SDK for Java depends on protobuf-java V2.4.1 and httpasyncclient V4.0.2. If your project—or any library your project depends on—also pulls in different versions of these artifacts, Maven loads conflicting class definitions at runtime, triggering the exception above.

Solution

Switch to the jar-with-dependencies classifier of the tablestore artifact. This variant uses package relocation to bundle protobuf-java and httpasyncclient under a renamed package namespace, so they no longer conflict with any other version of those libraries on the classpath.

Add the following to your pom.xml:

Note

The jar-with-dependencies classifier bundles protobuf-java and httpasyncclient via package relocation, removing the external dependency on both libraries.

<dependency>
    <groupId>com.aliyun.openservices</groupId>
    <artifactId>tablestore</artifactId>
    <version>Your current version</version>
    <classifier>jar-with-dependencies</classifier>
    <exclusions>
        <exclusion>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpasyncclient</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <!-- For example, the following libraries indirectly depend on Tablestore SDKs -->
    <groupId>com.aliyun.xxxxxxx</groupId>
    <artifactId>yyyyyy</artifactId>
    <version>zzzzzzz</version>
    <classifier>jar-with-dependencies</classifier>
    <exclusions>
        <!-- Remove the indirect dependency on the tablestore library -->
        <exclusion>
            <groupId>com.aliyun.openservices</groupId>
            <artifactId>tablestore</artifactId>
        </exclusion>
    </exclusions>
</dependency>