There are more AWS SDK examples available in the AWS Doc SDK Examples
ACM examples using SDK for Java 2.x
The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Java 2.x with ACM.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Topics
Actions
The following code example shows how to use AddTagsToCertificate
.
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * <p> * For more information, see the following documentation topic: * <p> * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class AddTagsToCertificate { public static void main(String[] args) { final String usage = """ Usage: <certArn> Where: certArn - the ARN of the certificate. """; if (args.length != 1) { System.out.println(usage); return; } String certArn = args[0]; addTags(certArn); } /** * Adds tags to a certificate in AWS Certificate Manager (ACM). * * @param certArn the Amazon Resource Name (ARN) of the certificate to add tags to */ public static void addTags(String certArn) { AcmClient acmClient = AcmClient.create(); List<Tag> expectedTags = List.of(Tag.builder().key("key").value("value").build()); AddTagsToCertificateRequest addTagsToCertificateRequest = AddTagsToCertificateRequest.builder() .certificateArn(certArn) .tags(expectedTags) .build(); try { acmClient.addTagsToCertificate(addTagsToCertificateRequest); System.out.println("Successfully added tags to a certificate"); } catch (AcmException e) { System.out.println(e.getMessage()); } } }
-
For API details, see AddTagsToCertificate in AWS SDK for Java 2.x API Reference.
-
The following code example shows how to use DeleteCertificate
.
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * <p> * For more information, see the following documentation topic: * <p> * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DeleteCert { public static void main(String[] args) { final String usage = """ Usage: <certArn> Where: certArn - the ARN of the certificate. """; if (args.length != 1) { System.out.println(usage); return; } String certArn = args[0]; deleteCertificate(certArn); } /** * Deletes an SSL/TLS certificate from the AWS Certificate Manager (ACM). * * @param certArn the Amazon Resource Name (ARN) of the certificate to be deleted */ public static void deleteCertificate( String certArn) { AcmClient acmClient = AcmClient.create(); DeleteCertificateRequest request = DeleteCertificateRequest.builder() .certificateArn(certArn) .build(); try { acmClient.deleteCertificate(request); System.out.println("The certificate was deleted"); } catch (AcmException e) { System.out.println(e.getMessage()); } } }
-
For API details, see DeleteCertificate in AWS SDK for Java 2.x API Reference.
-
The following code example shows how to use DescribeCertificate
.
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * <p> * For more information, see the following documentation topic: * <p> * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DescribeCert { public static void main(String[] args) { final String usage = """ Usage: <certArn> Where: certArn - the ARN of the certificate. """; if (args.length != 1) { System.out.println(usage); return; } String certArn = args[0]; describeCertificate(certArn); } /** * Describes the details of an SSL/TLS certificate. * * @param certArn the Amazon Resource Name (ARN) of the certificate to describe * @throws AcmException if an error occurs while describing the certificate */ public static void describeCertificate(String certArn) { AcmClient acmClient = AcmClient.create(); DescribeCertificateRequest req = DescribeCertificateRequest.builder() .certificateArn(certArn) .build(); try { DescribeCertificateResponse response = acmClient.describeCertificate(req); // Print the certificate details. System.out.println("Certificate ARN: " + response.certificate().certificateArn()); System.out.println("Domain Name: " + response.certificate().domainName()); System.out.println("Issued By: " + response.certificate().issuer()); System.out.println("Issued On: " + response.certificate().issuedAt()); System.out.println("Status: " + response.certificate().status()); } catch (AcmException e) { System.out.println(e.getMessage()); } } }
-
For API details, see DescribeCertificate in AWS SDK for Java 2.x API Reference.
-
The following code example shows how to use ExportCertificate
.
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * <p> * For more information, see the following documentation topic: * <p> * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ExportCertificate { public static void main(String[] args) throws Exception { final String usage = """ Usage: <certArn> Where: certArn - the ARN of the certificate. """; if (args.length != 1) { System.out.println(usage); return; } String certArn = args[0]; exportCert(certArn); } /** * Exports an SSL/TLS certificate and its associated private key and certificate chain from AWS Certificate Manager (ACM). * * @param certArn The Amazon Resource Name (ARN) of the certificate that you want to export. * @throws IOException If an I/O error occurs while reading the private key passphrase file or exporting the certificate. */ public static void exportCert(String certArn) throws IOException { AcmClient acmClient = AcmClient.create(); // Initialize a file descriptor for the passphrase file. RandomAccessFile filePassphrase = null; ByteBuffer bufPassphrase = null; // Create a file stream for reading the private key passphrase. try { filePassphrase = new RandomAccessFile("C:\\AWS\\password.txt", "r"); } catch (IllegalArgumentException | SecurityException | FileNotFoundException ex) { throw ex; } // Create a channel to map the file. FileChannel channelPassphrase = filePassphrase.getChannel(); // Map the file to the buffer. try { bufPassphrase = channelPassphrase.map(FileChannel.MapMode.READ_ONLY, 0, channelPassphrase.size()); channelPassphrase.close(); filePassphrase.close(); } catch (IOException ex) { throw ex; } // Create a request object. ExportCertificateRequest req = ExportCertificateRequest.builder() .certificateArn(certArn) .passphrase(SdkBytes.fromByteBuffer(bufPassphrase)) .build(); // Export the certificate. ExportCertificateResponse result = null; try { result = acmClient.exportCertificate(req); } catch (InvalidArnException | InvalidTagException | ResourceNotFoundException ex) { throw ex; } // Clear the buffer. bufPassphrase.clear(); // Display the certificate and certificate chain. String certificate = result.certificate(); System.out.println(certificate); String certificateChain = result.certificateChain(); System.out.println(certificateChain); // This example retrieves but does not display the private key. String privateKey = result.privateKey(); System.out.println("The example is complete"); } }
-
For API details, see ExportCertificate in AWS SDK for Java 2.x API Reference.
-
The following code example shows how to use ImportCertificate
.
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * <p> * For more information, see the following documentation topic: * <p> * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ImportCert { public static void main(String[] args) { final String usage = """ Usage: <certificatePath> <privateKeyPath> Where: certificatePath - the path to the SSL/TLS certificate file. privateKeyPath - the path to the private key file associated with the SSL/TLS certificate. """; if (args.length != 2) { System.out.println(usage); return; } String certificatePath = args[0]; String privateKeyPath = args[1]; String certificateArn = importCertificate(certificatePath, privateKeyPath); System.out.println("Certificate imported with ARN: " + certificateArn); } /** * Imports an SSL/TLS certificate and private key into AWS Certificate Manager (ACM) for use with * AWS services. * * @param certificatePath the file path to the SSL/TLS certificate * @param privateKeyPath the file path to the private key associated with the certificate * @throws IOException if there is an error reading the certificate or private key files */ public static String importCertificate(String certificatePath, String privateKeyPath) { AcmClient acmClient = AcmClient.create(); try { byte[] certificateBytes = readFileBytes(certificatePath); byte[] privateKeyBytes = readFileBytes(privateKeyPath); ImportCertificateRequest request = ImportCertificateRequest.builder() .certificate(SdkBytes.fromByteBuffer(ByteBuffer.wrap(certificateBytes))) .privateKey(SdkBytes.fromByteBuffer(ByteBuffer.wrap(privateKeyBytes))) .build(); ImportCertificateResponse response = acmClient.importCertificate(request); String certificateArn = response.certificateArn(); return certificateArn; } catch (IOException e) { System.err.println("Error reading certificate or private key file: " + e.getMessage()); } return ""; } private static byte[] readFileBytes(String filePath) throws IOException { try (InputStream inputStream = new FileInputStream(filePath)) { return IoUtils.toByteArray(inputStream); } } }
-
For API details, see ImportCertificate in AWS SDK for Java 2.x API Reference.
-
The following code example shows how to use ListCertificates
.
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * <p> * For more information, see the following documentation topic: * <p> * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListCerts { public static void main(String[] args) { listCertificates(); } /** * Lists all the certificates managed by AWS Certificate Manager (ACM) that have a status of "ISSUED". */ public static void listCertificates() { AcmClient acmClient = AcmClient.create(); try { ListCertificatesRequest listRequest = ListCertificatesRequest.builder() .certificateStatuses(CertificateStatus.ISSUED) .maxItems(100) .build(); ListCertificatesIterable listResponse = acmClient.listCertificatesPaginator(listRequest); // Print the certificate details using streams listResponse.certificateSummaryList().stream() .forEach(certificate -> { System.out.println("Certificate ARN: " + certificate.certificateArn()); System.out.println("Certificate Domain Name: " + certificate.domainName()); System.out.println("Certificate Status: " + certificate.statusAsString()); System.out.println("---"); }); } catch (AcmException e) { System.err.println(e.getMessage()); } } }
-
For API details, see ListCertificates in AWS SDK for Java 2.x API Reference.
-
The following code example shows how to use ListTagsForCertificate
.
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * <p> * For more information, see the following documentation topic: * <p> * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListCertTags { public static void main(String[] args) { final String usage = """ Usage: <certArn> Where: certArn - the ARN of the certificate. """; if (args.length != 1) { System.out.println(usage); return; } String certArn = args[0]; listCertTags(certArn); } /** * Lists the tags associated with an AWS Certificate Manager (ACM) certificate. * * @param certArn the Amazon Resource Name (ARN) of the ACM certificate */ public static void listCertTags(String certArn) { AcmClient acmClient = AcmClient.create(); ListTagsForCertificateRequest request = ListTagsForCertificateRequest.builder() .certificateArn(certArn) .build(); ListTagsForCertificateResponse response = acmClient.listTagsForCertificate(request); List<Tag> tagList = response.tags(); tagList.forEach(tag -> { System.out.println("Key: " + tag.key()); System.out.println("Value: " + tag.value()); }); } }
-
For API details, see ListTagsForCertificate in AWS SDK for Java 2.x API Reference.
-
The following code example shows how to use RemoveTagsFromCertificate
.
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * <p> * For more information, see the following documentation topic: * <p> * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class RemoveTagsFromCert { public static void main(String[] args) { final String usage = """ Usage: <certArn> Where: certArn - the ARN of the certificate. """; if (args.length != 1) { System.out.println(usage); return; } String certArn = args[0]; removeTags(certArn); } /** * Removes tags from an AWS Certificate Manager (ACM) certificate. * * @param certArn the Amazon Resource Name (ARN) of the certificate from which to remove tags */ public static void removeTags(String certArn) { AcmClient acmClient = AcmClient.create(); List<Tag> expectedTags = List.of(Tag.builder().key("key").value("value").build()); RemoveTagsFromCertificateRequest req = RemoveTagsFromCertificateRequest.builder() .certificateArn(certArn) .tags(expectedTags) .build(); try { acmClient.removeTagsFromCertificate(req); System.out.println("Successfully removed tags from the certificate"); } catch (AcmException e) { System.err.println(e.getMessage()); } } }
-
For API details, see RemoveTagsFromCertificate in AWS SDK for Java 2.x API Reference.
-
The following code example shows how to use RenewCertificate
.
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * <p> * For more information, see the following documentation topic: * <p> * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class RenewCert { public static void main(String[] args) { final String usage = """ Usage: <certArn> Where: certArn - the ARN of the certificate. """; if (args.length != 1) { System.out.println(usage); return; } String certArn = args[0]; renewCertificate(certArn); } /** * Renews an existing SSL/TLS certificate in AWS Certificate Manager (ACM). * * @param certArn The Amazon Resource Name (ARN) of the certificate to be renewed. * @throws AcmException If there is an error renewing the certificate. */ public static void renewCertificate(String certArn) { AcmClient acmClient = AcmClient.create(); RenewCertificateRequest certificateRequest = RenewCertificateRequest.builder() .certificateArn(certArn) .build(); try { acmClient.renewCertificate(certificateRequest); System.out.println("The certificate was renewed"); } catch(AcmException e){ System.out.println(e.getMessage()); } } }
-
For API details, see RenewCertificate in AWS SDK for Java 2.x API Reference.
-
The following code example shows how to use RequestCertificate
.
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * <p> * For more information, see the following documentation topic: * <p> * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class RequestCert { public static void main(String[] args) { requestCertificate(); } /** * Requests a certificate from the AWS Certificate Manager (ACM) service. */ public static void requestCertificate() { AcmClient acmClient = AcmClient.create(); ArrayList<String> san = new ArrayList<>(); san.add("www.example.com"); RequestCertificateRequest req = RequestCertificateRequest.builder() .domainName("example.com") .idempotencyToken("1Aq25pTy") .subjectAlternativeNames(san) .build(); try { RequestCertificateResponse response = acmClient.requestCertificate(req); System.out.println("Cert ARN IS " + response.certificateArn()); } catch (AcmException e) { System.err.println(e.getMessage()); } } }
-
For API details, see RequestCertificate in AWS SDK for Java 2.x API Reference.
-