Search code examples
javaamazon-web-servicesamazon-ec2nosuchmethoderroraws-sdk-java-2.0

Create Tags failing with error java.lang.NoSuchMethodError


I am able to create instances using aws-sdk-java, but recently when trying to create tags for naming the instances I am repeatedly getting the error

java.lang.NoSuchMethodError: com.amazonaws.services.ec2.AmazonEC2.createTags(Lcom/amazonaws/services/ec2/model/CreateTagsRequest;)Lcom/amazonaws/services/ec2/model/CreateTagsResult;

            Tag resourceName = new Tag("Name", instanceName);
            List<Tag> tags = new ArrayList<Tag>();
            tags.add(resourceName);
            CreateTagsRequest tag_request = new CreateTagsRequest()
                    .withTags(resourceName).withResources("i-0xxxxxxxxxxxxxxxx");

            try {
                CreateTagsResult tag_response = ec2Client.createTags(tag_request);
            }catch(com.amazonaws.services.ec2.model.AmazonEC2Exception e){
                e.printStackTrace();
            }

I tried setting tag name while creating the instance using

        List<TagSpecification> tagSpecifications = new ArrayList<>();
        tagSpecifications.add(new TagSpecification().withTags(tags));
        runInstancesRequest.withTagSpecifications(tagSpecifications);

But even that is failing with same error java.lang.NoSuchMethodError. While compilation there is no issue, then I do not understand why there is this error after deployment.


Solution

  • Either the method name createTags does not exist within the class AmazonEC2 or you pass a wrong parameter list to the method.

    This can also happen if the client and server side version of the aws-sdk-java api differ from each other. Be sure that you use the correct client side API.

    The AmazonE2Client is mostly deprecated: @see https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/ec2/AmazonEC2Client.html

    The documentation says "use AWSClientBuilder instead"