调用与Azure存储相关的Java API时引发的异常java.lang.NoSuchMethodError [英] The exception java.lang.NoSuchMethodError thrown when invoking Azure storage related java API

查看:282
本文介绍了调用与Azure存储相关的Java API时引发的异常java.lang.NoSuchMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里为其他可能遇到相同问题的人保留线程.

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

我正在尝试通过以下代码从Azure 容器中读取 blob :

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());
    }
}

但是,当它执行 blobContainerClient.listBlobs()时,抛出以下异常:

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;

我正在使用 maven 作为构建工具.

I'm using maven as the build tool.

这里会发生什么?

推荐答案

我终于找到了解决方案,它是关于 maven 依赖项冲突的.一个以上的依赖项依赖于不同版本中的 netty .

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

我在maven中同时添加了 aws azure 依赖项,如下所示:

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>

通过使用Maven工具 mvndependency:tree ,我得到如下输出:

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

我们可以看到, azure aws 确实取决于 netty ,并且 netty 的版本不同.因此,问题在于解决冲突.

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.

根据 maven 的介绍a>,

As per introduciton from maven,

由于Maven可传递地解决依赖关系,因此有可能不需要的依赖项将包含在项目的类路径中.为了例如,某个较旧的jar可能存在安全问题,或者与您使用的Java版本不兼容.为了解决这个问题,Maven允许您排除特定的依赖关系.设置了排除项取决于您的POM中的特定依赖项,并且针对特定的groupId和artifactId.当您构建项目时,该工件不会通过依赖关系添加到项目的类路径中在其中声明了排除规则.

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.

我们需要排除 netty 4.1.17,以免将其添加到项目的类路径中,并将 netty 设置为 azure .

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>

通过将以上依赖项添加到 pom.xml 中, azure 可以正常工作.

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

这篇关于调用与Azure存储相关的Java API时引发的异常java.lang.NoSuchMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆