//snippet-sourcedescription:[UpdateUser.java demonstrates how to update the name of an IAM user.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[iam]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[]
//snippet-sourceauthor:[soo-aws]
/*
 * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.example.iam;
// snippet-start:[iam.java2.update_user.complete]
// snippet-start:[iam.java2.update_user.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.iam.IamClient;
import software.amazon.awssdk.services.iam.model.UpdateUserRequest;
import software.amazon.awssdk.services.iam.model.UpdateUserResponse;
 
// snippet-end:[iam.java2.update_user.import]
/**
 * Updates an IAM user's username
 */
public class UpdateUser {
    public static void main(String[] args) {

        final String USAGE =
            "To run this example, supply the current username and a new\n" +
            "username. Ex:\n\n" +
            "UpdateUser <current-name> <new-name>\n";

        if (args.length != 1) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String cur_name = args[0];
        String new_name = args[1];

        // snippet-start:[iam.java2.update_user.main]
        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient.builder().region(region).build();

        UpdateUserRequest request = UpdateUserRequest.builder()
            .userName(cur_name)
            .newUserName(new_name).build();

        UpdateUserResponse response = iam.updateUser(request);
        // snippet-end:[iam.java2.update_user.main]

        System.out.printf("Successfully updated user to username %s",
                new_name);
    }
}
 
// snippet-end:[iam.java2.update_user.complete]

@@ -247,16 +247,26 @@
 cedate:%5B
+03/02/2020
 %5D%0D%0A//sni
@@ -289,10 +289,15 @@
 r:%5Bs
-oo
+cmacdon
 -aws
@@ -320,18 +320,8 @@
 ght 
-2010-2019 
 Amaz
@@ -1112,24 +1112,88 @@
 IamClient;%0D%0A
+import software.amazon.awssdk.services.iam.model.IamException;%0D%0A
 import softw
@@ -1319,19 +1319,16 @@
 ponse;%0D%0A
- %0D%0A
 // snipp
@@ -1366,16 +1366,18 @@
 mport%5D%0D%0A
+%0D%0A
 /**%0D%0A * 
@@ -1530,16 +1530,20 @@
         
+    
 %22To run 
@@ -1596,24 +1596,36 @@
 a new%5Cn%22 +%0D%0A
+            
             
@@ -1643,24 +1643,36 @@
 Ex:%5Cn%5Cn%22 +%0D%0A
+            
             
@@ -1849,18 +1849,17 @@
 ring cur
-_n
+N
 ame = ar
@@ -1884,18 +1884,17 @@
 ring new
-_n
+N
 ame = ar
@@ -2049,23 +2049,59 @@
 er()
-.region(region)
+%0D%0A                .region(region)%0D%0A                
 .bui
@@ -2101,32 +2101,51 @@
    .build();%0D%0A%0D%0A
+        try %7B%0D%0A    
         UpdateUs
@@ -2205,16 +2205,20 @@
         
+    
 .userNam
@@ -2226,16 +2226,19 @@
 (cur
-_n
+N
 ame)%0D%0A
+    
     
@@ -2261,18 +2261,17 @@
 Name(new
-_n
+N
 ame).bui
@@ -2271,32 +2271,36 @@
 me).build();%0D%0A%0D%0A
+    
         UpdateUs
@@ -2347,32 +2347,36 @@
 uest);%0D%0A        
+   
+ 
 // snippet-end:%5B
@@ -2402,24 +2402,28 @@
 er.main%5D%0D%0A%0D%0A
+    
         Syst
@@ -2504,21 +2504,154 @@
  new
-_name
+Name);%0D%0A        %7D catch (IamException e) %7B%0D%0A            System.err.println(e.awsErrorDetails().errorMessage());%0D%0A            System.exit(1
 );%0D%0A    
 %7D%0D%0A%7D
@@ -2650,15 +2650,64 @@
     
+  
+  
 %7D%0D%0A
-%7D%0D%0A 
+        System.out.println(%22Done%22);%0D%0A        %7D%0D%0A%7D
 %0D%0A//

Update UpdateUser.java