The exception java.lang.NoSuchMethodError thrown when invoking Azure storage related java API

怎甘沉沦 提交于 2021-01-01 06:56:33

问题


Leave thread here for others who might run into same issues.


I'm trying to reading blob from Azure container by code below:

public static void main(String[] args) {
    String connectStr = "it's a workable connection string...";
    // Create a BlobServiceClient object which will be used to create a container client
    BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient();
    String containerName = "eugenecontainer";
    BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient(containerName);
    for (BlobItem blobItem: blobContainerClient.listBlobs()){
        System.out.println(blobItem.getName());
    }
}

However, when it executes blobContainerClient.listBlobs(), exception as following throws:

Exception in thread "main" java.lang.NoSuchMethodError: io.netty.bootstrap.Bootstrap.config()Lio/netty/bootstrap/BootstrapConfig;

I'm using maven as the build tool.

What happens here?


回答1:


I finally found the solution and it's about the maven dependency conflict. More than one dependencies depend on netty in different versions.

I have added both aws and azure dependency in maven like below:

<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-java-sdk</artifactId>
  <version>1.11.327</version>
</dependency>
<dependency>
  <groupId>com.azure</groupId>
  <artifactId>azure-storage-blob</artifactId>
  <version>12.0.0</version>
</dependency>

By using maven tool mvn dependency:tree, I got output as following:

[INFO] |  +- com.amazonaws:aws-java-sdk-kinesisvideo:jar:1.11.327:compile
[INFO] |  |  +- io.netty:netty-codec-http:jar:4.1.17.Final:compile
[INFO] |  |  |  \- io.netty:netty-codec:jar:4.1.17.Final:compile
[INFO] |  |  \- io.netty:netty-handler:jar:4.1.17.Final:compile
[INFO] |  |     +- io.netty:netty-buffer:jar:4.1.17.Final:compile
[INFO] |  |     |  \- io.netty:netty-common:jar:4.1.17.Final:compile
[INFO] |  |     \- io.netty:netty-transport:jar:4.1.17.Final:compile
[INFO] |  |        \- io.netty:netty-resolver:jar:4.1.17.Final:compile
[INFO] |  \- com.azure:azure-storage-common:jar:12.0.0:compile
[INFO] |     \- com.azure:azure-core-http-netty:jar:1.0.0:compile
[INFO] |        +- io.netty:netty-handler-proxy:jar:4.1.42.Final:compile
[INFO] |        |  \- io.netty:netty-codec-socks:jar:4.1.42.Final:compile
[INFO] |        +- io.projectreactor.netty:reactor-netty:jar:0.9.0.RELEASE:compile
[INFO] |        |  +- io.netty:netty-codec-http2:jar:4.1.39.Final:compile
[INFO] |        |  +- io.netty:netty-transport-native-epoll:jar:linux-x86_64:4.1.39.Final:compile
[INFO] |        |  |  \- io.netty:netty-transport-native-unix-common:jar:4.1.39.Final:compile
[INFO] |        |  \- io.projectreactor.addons:reactor-pool:jar:0.1.0.RELEASE:compile
[INFO] |        \- com.azure:azure-core-test:jar:1.0.0:compile
[INFO] |           \- io.projectreactor:reactor-test:jar:3.3.0.RELEASE:compile

As we can see, azure and aws did depend on netty and the version of netty is different. So the question is about solving the conflict.

As per introduciton from maven,

Since Maven resolves dependencies transitively, it is possible for unwanted dependencies to be included in your project's classpath. For example, a certain older jar may have security issues or be incompatible with the Java version you're using. To address this, Maven allows you to exclude specific dependencies. Exclusions are set on a specific dependency in your POM, and are targeted at a specific groupId and artifactId. When you build your project, that artifact will not be added to your project's classpath by way of the dependency in which the exclusion was declared.

We need to exclude netty 4.1.17 so that it won't be added to project's classpath and set netty to azure explicitly.

    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk</artifactId>
      <version>1.11.327</version>
      <exclusions>
        <exclusion>
          <artifactId>*</artifactId>
          <groupId>io.netty</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>com.azure</groupId>
      <artifactId>azure-storage-blob</artifactId>
      <version>12.0.0</version>
    </dependency>
    <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-all</artifactId>
      <version>4.1.42.Final</version>
    </dependency>

By adding above dependencies to pom.xml, the azure works fine then.



来源:https://stackoverflow.com/questions/60589689/the-exception-java-lang-nosuchmethoderror-thrown-when-invoking-azure-storage-rel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!