How do I run an AWS Batch job using Java?

Last updated: 2020-10-30

I want to run AWS Batch jobs using Java.


Prepare your environment

1.    To install Java, follow the download instructions on the Oracle website.

2.    To install Eclipse, follow the download instructions on the Eclipse Foundation website.

3.    (Required) Create an AWS Batch compute environment, job definition, and job queue.

4.    To verify that the job queue is assigned to a valid compute environment, run the following command:

$ aws batch describe-compute-environments --compute-environments your-compute-env-name

Important: You must assign your AWS Batch job queue to a valid compute environment. 

5.    Verify that the status of the compute environment is valid.

If your compute environment is invalid, fix the invalid compute environment before moving forward with the following steps.

Note: You can use Java code to create an AWS Batch compute environment, job definition, and job queue. For more information, see the sample code from the AWS SDK for Java API Reference. If you use Java code to create a compute environment, job definition, and job queue, then you must do it after completing the steps in the Convert a Java project to a Maven project section.

Install the AWS Toolkit for Eclipse

1.    Open the Eclipse IDE.

2.    From the Eclipse IDE menu bar, choose Help.

3.    In Help, choose Eclipse Marketplace

4.    In Eclipse Marketplace, choose the Search tab.

5.    In the Find search box, enter AWS.

6.    From the search results, choose Install for AWS Toolkit for Eclipse.

7.    From the Eclipse menu bar, choose Navigate.

8.    Choose Preferences, Add Access Key ID, Secret Access key

Create a new Java project

1.   From the Eclipse IDE menu bar, choose File.

2.    Choose New, and then choose Project.

Convert a Java project to a Maven project

1.    In the Eclipse IDE, right-click the Java project that you created.

2.    Choose Configure, and then choose Convert to Maven Project

Note: After the project is converted to Maven, it creates a POM.xml file. Maven is a build automation tool used for Java projects. A Project Object Model (POM) is an XML file that contains information about the project and configuration details used by Maven to build the project.

3.    In the POM.xml file, add the following code after the closing build tag:

   <!-- -->

Important: The preceding code for step 3 works only if you include the code after the closing build tag in the POM.xml file.

Create a java program to submit AWS Batch jobs

1.    In the Eclipse IDE, choose the project that you created.

2.    Inside the project, right-click the src folder, choose New, and then choose File.

3.    Name the file

Note: The .java extension name must match with the public class name in the Java program.

4.    In the file, enter the following code:

public class BatchClient {
public static void main(String[] args) {
        AWSBatch client = AWSBatchClientBuilder.standard().withRegion("us-east-1").build();
SubmitJobRequest request = new SubmitJobRequest().withJobName("example").withJobQueue("new-queue").withJobDefinition("sleep30:4");
SubmitJobResult response = client.submitJob(request);

Important: In the preceding code, replace the queue name (new-queue), AWS Region (us-east-1), and job definition (sleep30:4) with the correct values for your AWS Batch environment. 

Note: If you receive the "SubmitJobResult can not be resolved" error, then you must import the package required for the SubmitJobResult API action. To import the package in the Eclipse IDE, in the Java code, select the SubmitJobResult, and then right-click Choose, Source, Add import.

5.    To submit the AWS Batch job and run the Java program, choose Run from the Run menu.

Did this article help?

Do you need billing or technical support?