@@ -0,0 +1,3101 @@
+/**%0A * Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.%0A *%0A * This file is licensed under the Apache License, Version 2.0 (the %22License%22).%0A * You may not use this file except in compliance with the License. A copy of%0A * the License is located at%0A *%0A * http://aws.amazon.com/apache2.0/%0A *%0A * This file is distributed on an %22AS IS%22 BASIS, WITHOUT WARRANTIES OR%0A * CONDITIONS OF ANY KIND, either express or implied. See the License for the%0A * specific language governing permissions and limitations under the License.%0A *%0A */%0A%0A// snippet-comment:%5BThese are tags for the AWS doc team's sample catalog. Do not remove.%5D%0A// snippet-sourcedescription:%5BFindRunningInstances.java demonstrates how to use a Filter to find running instances%5D%0A// snippet-service:%5Bec2%5D%0A// snippet-keyword:%5BJava%5D%0A// snippet-keyword:%5BCode Sample%5D%0A// snippet-sourcetype:%5Bfull-example%5D%0A// snippet-sourcedate:%5B2020-01-10%5D%0A// snippet-sourceauthor:%5BAWS-scmacdon%5D%0A%0A// snippet-start:%5Bec2.java1.running_instances.complete%5D%0Apackage aws.example.ec2;%0A%0A// snippet-start:%5Bec2.java1.running_instances.import%5D%0Aimport com.amazonaws.services.ec2.AmazonEC2;%0Aimport com.amazonaws.services.ec2.AmazonEC2ClientBuilder;%0Aimport com.amazonaws.services.ec2.model.*;%0Aimport com.amazonaws.services.ec2.model.DescribeInstancesResult;%0A// snippet-end:%5Bec2.java1.running_instances.import%5D%0A%0Apublic class FindRunningInstances%7B%0A%0A    public static void main(String%5B%5D args) %7B%0A%0A        // snippet-start:%5Bec2.java1.running_instances.main%5D%0A        AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();%0A%0A        try %7B%0A            //Create the Filter to use to find running instances%0A            Filter filter = new Filter(%22instance-state-name%22);%0A            filter.withValues(%22running%22);%0A%0A            //Create a DescribeInstancesRequest%0A            DescribeInstancesRequest request = new DescribeInstancesRequest();%0A            request.withFilters(filter);%0A%0A            // Find the running instances%0A            DescribeInstancesResult response = ec2.describeInstances(request);%0A%0A            for (Reservation reservation : response.getReservations())%7B%0A%0A                for (Instance instance : reservation.getInstances()) %7B%0A%0A                   //Print out the results%0A                    System.out.printf(%0A                            %22Found reservation with id %25s, %22 +%0A                                    %22AMI %25s, %22 +%0A                                    %22type %25s, %22 +%0A                                    %22state %25s %22 +%0A                                    %22and monitoring state %25s%22,%0A                            instance.getInstanceId(),%0A                            instance.getImageId(),%0A                            instance.getInstanceType(),%0A                            instance.getState().getName(),%0A                            instance.getMonitoring().getState());%0A               %7D%0A            %7D%0A           System.out.print(%22Done%22);%0A        %7D%0A        catch (Exception e)%0A        %7B%0A            e.getStackTrace();%0A        %7D%0A        // snippet-end:%5Bec2.java1.running_instances.main%5D%0A    %7D%0A%7D%0A// snippet-end:%5Bec2.java1.running_instances.complete%5D%0A

Create FindRunningInstances.java