DOP-C02 SDLC Automation Practice Question
A development team uses AWS CodeBuild to compile a Java application. The build fails during the 'Install' phase with an error: 'Error: JAVA_HOME is not set'. How should the team fix this?
⚠ Common exam trap
Test-takers frequently assume using a managed image with Java pre-installed automatically sets JAVA_HOME, but AWS CodeBuild images do not always export this variable by default, requiring explicit definition in the buildspec.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Set the environment variable 'JAVA_HOME' in the buildspec file's 'env' section.
The error 'JAVA_HOME is not set' indicates that the build environment lacks the required environment variable pointing to the Java installation. In AWS CodeBuild, the buildspec file's 'env' section allows you to define environment variables, including 'JAVA_HOME', which can be set to the path of the Java runtime (e.g., '/usr/lib/jvm/java-11-openjdk-amd64'). This ensures the variable is available during the Install phase, resolving the error without altering the build image or adding installation steps.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Set the environment variable 'JAVA_HOME' in the buildspec file's 'env' section.
Why this is correct
Setting JAVA_HOME in the buildspec's env section ensures the variable is exported into every build phase's shell before commands run. This resolves the 'invalid JAVA_HOME' error because Maven or Gradle translates this variable into the absolute path of the JDK installation, which is needed for compilation. It's the canonical fix and works regardless of which base image CodeBuild uses.
- ✗
Use the managed image 'aws/codebuild/standard:5.0' which has Java pre-installed.
Why it's wrong here
Switching to aws/codebuild/standard:5.0 does not automatically define JAVA_HOME; the image merely makes multiple JDK versions available on the PATH. When you don't explicitly set JAVA_HOME, CodeBuild's environment may inherit a default or leave it undefined, and a Spring Boot build will still fail if the variable is missing or points to the wrong JDK. Therefore, even on this managed image, you must still define JAVA_HOME in the buildspec.
- ✗
Install Java in the pre_build phase using a command.
Why it's wrong here
Running apt-get or yum to install Java in pre_build only places the JDK binaries under /usr/lib/jvm or similar, but it never exports the JAVA_HOME environment variable to the build process. Because each build phase runs in a fresh shell process, any export you add to pre_build won't survive into the build phase unless you also append it to ~/.bashrc or set it via the buildspec's env section. This option leaves the root cause untouched and would still produce the same JAVA_HOME error.
- ✗
Use a custom Docker image that has Java pre-installed.
Why it's wrong here
Building a custom Docker image that already contains Java does not address the missing JAVA_HOME variable; the image only provides the installed filesystem, and CodeBuild still starts with a default environment that lacks a defined JAVA_HOME unless the image's Dockerfile has an ENV JAVA_HOME instruction. You would need to verify the image's environment or explicitly set JAVA_HOME in the buildspec, so this option alone doesn't fix the failure. It also introduces extra maintenance and still requires the same configuration.
Visual reference
Go deeper
Related to this question
About these practice questions
Courseiva writes every DOP-C02 question from scratch — 251 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This DOP-C02 practice question is part of Courseiva's free Amazon Web Services certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the DOP-C02 exam.