- Java tutorial for AWS Cloud9
- Prerequisites
- Step 1: Install required tools
- Step 2: Add code
- Step 3: Build and run the code
- Step 4: Set up to use the AWS SDK for Java
- Set up with Maven
- Set up with Gradle
- Step 5: Set up AWS credentials management in your environment
- Step 6: Add AWS SDK code
- Step 7: Build and run the AWS SDK code
- Step 8: Clean up
Java tutorial for AWS Cloud9
If you’re using an AWS Cloud9 development environment that’s backed by an EC2 instance with 2 GiB or more of memory, we recommend that you activate enhanced Java support. This provides access to productivity features such as code completion, linting for errors, context-specific actions, and debugging options such as breakpoints and stepping.
This tutorial enables you to run some Java code in an AWS Cloud9 development environment.
Following this tutorial and creating this sample might result in charges to your AWS account. These include possible charges for services such as Amazon EC2 and Amazon S3. For more information, see Amazon EC2 Pricing and Amazon S3 Pricing .
Topics
Prerequisites
Before you use this sample, make sure that your setup meets the following requirements:
- You must have an existing AWS Cloud9 EC2 development environment. This sample assumes that you already have an EC2 environment that’s connected to an Amazon EC2 instance that runs Amazon Linux or Ubuntu Server. If you have a different type of environment or operating system, you might need to adapt this sample’s instructions to set up related tools. For more information, see Creating an environment in AWS Cloud9.
- You have the AWS Cloud9 IDE for the existing environment already open. When you open an environment, AWS Cloud9 opens the IDE for that environment in your web browser. For more information, see Opening an environment in AWS Cloud9.
Step 1: Install required tools
In this step, you install a set of Java development tools in your AWS Cloud9 development environment. If you already have a set of Java development tools such as the Oracle JDK or OpenJDK installed in your environment, you can skip ahead to Step 2: Add code. This sample was developed with OpenJDK 8, which you can install in your environment by completing the following procedure.
- Confirm whether OpenJDK 8 is already installed. To do this, in a terminal session in the AWS Cloud9 IDE, run the command line version of the Java runner with the -version option. (To start a new terminal session, on the menu bar, choose Window, New Terminal.)
- If the output states that the java command isn’t found, continue with step 2 in this procedure to install OpenJDK 8.
- If the output contains values starting with Java(TM) , Java Runtime Environment , Java SE , J2SE , or Java2 , the OpenJDK isn’t installed or isn’t set as the default Java development toolset. Continue with step 2 in this procedure to install OpenJDK 8, and then switch to using OpenJDK 8.
- If the output contains values starting with java version 1.8 and OpenJDK , skip ahead to Step 2: Add code. OpenJDK 8 is installed correctly for this sample.
- If the output contains a java version less than 1.8 and values starting with OpenJDK , continue with step 2 in this procedure to upgrade the installed OpenJDK version to OpenJDK 8.
sudo yum -y install java-1.8.0-openjdk-devel
sudo apt install -y openjdk-8-jdk
sudo update-alternatives --config java sudo update-alternatives --config javac
java -version javac -version
Step 2: Add code
In the AWS Cloud9 IDE, create a file with the following code, and save the file with the name hello.java . (To create a file, on the menu bar, choose File, New File. To save the file, choose File, Save.)
public class hello public static void main(String []args) System.out.println("Hello, World!"); System.out.println("The sum of 2 and 3 is 5."); int sum = Integer.parseInt(args[0]) + Integer.parseInt(args[1]); System.out.format("The sum of %s and %s is %s.\n", args[0], args[1], Integer.toString(sum)); > >
Step 3: Build and run the code
- Use the command line version of the Java compiler to compile the hello.java file into a hello.class file. To do this, using the terminal in the AWS Cloud9 IDE, from the same directory as the hello.java file, run the Java compiler, specifying the hello.java file.
Hello, World! The sum of 2 and 3 is 5. The sum of 5 and 9 is 14.
Step 4: Set up to use the AWS SDK for Java
You can enhance this sample to use the AWS SDK for Java to create an Amazon S3 bucket, list your available buckets, and then delete the bucket you just created.
In this step, you install Apache Maven or Gradle in your environment. Maven and Gradle are common build automation systems that can be used with Java projects. After you install Maven or Gradle, you use it to generate a new Java project. In this new project, you add a reference to the AWS SDK for Java. This AWS SDK for Java provides a convenient way to interact with AWS services such as Amazon S3, from your Java code.
Topics
Set up with Maven
- Install Maven in your environment. To see whether Maven is already installed, using the terminal in the AWS Cloud9 IDE, run Maven with the -version option.
sudo wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo sudo sed -i s/\$releasever/6/g /etc/yum.repos.d/epel-apache-maven.repo sudo yum install -y apache-maven
For more information about the preceding commands, see Extra Packages for Enterprise Linux (EPEL) on the Fedora Project Wiki website. For Ubuntu Server, run the following command instead.
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
my-app |- src | `- main | `- java | `- com | `- mycompany | `- app | `-App.java |- test | `- java | `- com | `- mycompany | `- app | `- AppTest.java `- pom.xml
4.0.0 com.mycompany.app my-app jar 1.0-SNAPSHOT org.apache.maven.plugins maven-assembly-plugin 3.6.0 jar-with-dependencies com.mycompany.app.App package single junit junit 3.8.1 test com.amazonaws aws-java-sdk 1.11.330
- The artifactid setting of my-app sets the project’s root directory name, and the group-id setting of com.mycompany.app sets the com/mycompany/app subdirectory structure and the package declaration in the App.Java and AppTest.java files.
- The artifactId setting of my-app , with the packaging setting of jar , the version setting of 1.0-SNAPSHOT , and the descriptorRef setting of jar-with-dependencies set the output JAR file’s name of my-app-1.0-SNAPSHOT-jar-with-dependencies.jar .
- The plugin section declares that a single JAR, which includes all dependencies, will be built.
- The dependency section with the groupId setting of com.amazon.aws and the artifactId setting of aws-java-sdk includes the AWS SDK for Java library files. The AWS SDK for Java version to use is declared by the version setting. To use a different version, replace this version number.
Set up with Gradle
- Install Gradle in your environment. To see whether Gradle is already installed, using the terminal in the AWS Cloud9 IDE, run Gradle with the -version option.
curl -s "https://get.sdkman.io" | bash source "$HOME/.sdkman/bin/sdkman-init.sh" sdk install gradle
gradle init --type java-application
my-app |- .gradle | `- (various supporting project folders and files) |- gradle | `- (various supporting project folders and files) |- src | |- main | | `- java | | `- App.java | `- test | `- java | `- AppTest.java |- build.gradle |- gradlew |- gradlew.bat `- settings.gradle
import org.junit.Test; import static org.junit.Assert.*; public class AppTest @Test public void testAppExists () try Class.forName("com.mycompany.app.App"); > catch (ClassNotFoundException e) fail("Should have a class named App."); > > >
apply plugin: 'java' apply plugin: 'application' repositories jcenter() mavenCentral() > buildscript repositories mavenCentral() > dependencies classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE" > > apply plugin: "io.spring.dependency-management" dependencyManagement imports mavenBom 'com.amazonaws:aws-java-sdk-bom:1.11.330' > > dependencies compile 'com.amazonaws:aws-java-sdk-s3' testCompile group: 'junit', name: 'junit', version: '4.12' > run if (project.hasProperty("appArgs")) args Eval.me(appArgs) > > mainClassName = 'App'
The preceding build.gradle file includes project settings that specify declarations such as the following:
- The io.spring.dependency-management plugin is used to import the AWS SDK for Java Maven Bill of Materials (BOM) to manage AWS SDK for Java dependencies for the project. classpath declares the version to use. To use a different version, replace this version number.
- com.amazonaws:aws-java-sdk-s3 includes the Amazon S3 portion of the AWS SDK for Java library files. mavenBom declares the version to use. If you want to use a different version, replace this version number.
Step 5: Set up AWS credentials management in your environment
Each time you use the AWS SDK for Java to call an AWS service, you must provide a set of AWS credentials with the call. These credentials determine whether the AWS SDK for Java has the appropriate permissions to make that call. If the credentials don’t cover the appropriate permissions, the call will fail.
In this step, you store your credentials within the environment. To do this, follow the instructions in Calling AWS services from an environment in AWS Cloud9, and then return to this topic.
For additional information, see Set up AWS Credentials and Region for Development in the AWS SDK for Java Developer Guide.
Step 6: Add AWS SDK code
In this step, you add code to interact with Amazon S3 to create a bucket, list your available buckets, and then delete the bucket you just created.
From the Environment window, open the my-app/src/main/java/com/mycompany/app/App.java file for Maven or the my-app/src/main/java/App.java file for Gradle. In the editor, replace the file’s current contents with the following code, and then save the App.java file.
package com.mycompany.app; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.AmazonS3Exception; import com.amazonaws.services.s3.model.Bucket; import com.amazonaws.services.s3.model.CreateBucketRequest; import java.util.List; public class App private static AmazonS3 s3; public static void main(String[] args) if (args.length < 2) System.out.format("Usage: \n" + "Example: my-test-bucket us-east-2\n"); return; > String bucket_name = args[0]; String region = args[1]; s3 = AmazonS3ClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(region) .build(); // List current buckets. ListMyBuckets(); // Create the bucket. if (s3.doesBucketExistV2(bucket_name)) System.out.format("\nCannot create the bucket. \n" + "A bucket named '%s' already exists.", bucket_name); return; > else try System.out.format("\nCreating a new bucket named '%s'. \n\n", bucket_name); s3.createBucket(new CreateBucketRequest(bucket_name, region)); > catch (AmazonS3Exception e) System.err.println(e.getErrorMessage()); > > // Confirm that the bucket was created. ListMyBuckets(); // Delete the bucket. try System.out.format("\nDeleting the bucket named '%s'. \n\n", bucket_name); s3.deleteBucket(bucket_name); > catch (AmazonS3Exception e) System.err.println(e.getErrorMessage()); > // Confirm that the bucket was deleted. ListMyBuckets(); > private static void ListMyBuckets() List buckets = s3.listBuckets(); System.out.println("My buckets now are:"); for (Bucket b : buckets) System.out.println(b.getName()); > > >
Step 7: Build and run the AWS SDK code
To run the code from the previous step, run the following commands from the terminal. These commands use Maven or Gradle to create an executable JAR file for the project, and then use the Java runner to run the JAR. The JAR runs with the name of the bucket to create in Amazon S3 (for example, my-test-bucket ) and the ID of the AWS Region to create the bucket in as input (for example, us-east-2 ).
For Maven, run the following commands.
cd my-app mvn package java -cp target/my-app-1.0-SNAPSHOT-jar-with-dependencies.jar com.mycompany.app.App my-test-bucket us-east-2
For Gradle, run the following commands.
gradle build gradle run -PappArgs="['my-test-bucket', 'us-east-2']"
Compare your results to the following output.
My buckets now are: Creating a new bucket named 'my-test-bucket'. My buckets now are: my-test-bucket Deleting the bucket named 'my-test-bucket'. My buckets now are:
Step 8: Clean up
To prevent ongoing charges to your AWS account after you’re done using this sample, you should delete the environment. For instructions, see Deleting an environment in AWS Cloud9.