Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 322
Eugene Avatar asked May 11 '26 23:05

Eugene


2 Answers

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.

like image 84
Eugene Avatar answered May 14 '26 13:05

Eugene


If you are using spring boot dependencies as mentioned below, please exclude the azure-core-http-netty and add new individual dependency for azure-core-http-okhttp as follows.

<dependency>
                <groupId>com.azure.spring</groupId>
                <artifactId>azure-spring-boot-starter-storage</artifactId>
                <exclusions>
                    <exclusion>
                      <groupId>com.azure</groupId>
                      <artifactId>azure-core-http-netty</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
           <dependency>
               <groupId>com.azure</groupId>
               <artifactId>azure-core-http-okhttp</artifactId>
               <version>1.2.1</version>
           </dependency>
like image 26
SarangRN Avatar answered May 14 '26 11:05

SarangRN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!