nerdexam
CompTIA

XK0-004 · Question #383

A Linux administrator is running a script that runs a Java application but receives the following error: Error: Java_Home is not set Java is installed on the Linux server and java -version returns a v

The correct answer is D. Set JAVA_HOME -which java. The JAVA_HOME variable must be explicitly assigned in the script using command substitution against 'which java' so the application can locate the Java installation at runtime.

Scripting, Containers and Automation

Question

A Linux administrator is running a script that runs a Java application but receives the following error:

Error: Java_Home is not set Java is installed on the Linux server and java -version returns a value. Which of the following should the administrator add to the script to resolve this issue?

Options

  • AExport PATH-$PATH: $JAVA_HOME
  • BEcho `which java > $JAVA_HOME
  • CExport Java_Home which java
  • DSet JAVA_HOME -which java

How the community answered

(49 responses)
  • A
    6% (3)
  • B
    2% (1)
  • C
    2% (1)
  • D
    90% (44)

Why each option

The JAVA_HOME variable must be explicitly assigned in the script using command substitution against 'which java' so the application can locate the Java installation at runtime.

AExport PATH-$PATH: $JAVA_HOME

Exporting PATH=$PATH:$JAVA_HOME appends JAVA_HOME to the existing PATH but does not define or assign a value to JAVA_HOME itself, so the original error would remain.

BEcho `which java > $JAVA_HOME

Using 'echo `which java` > $JAVA_HOME' redirects the java path string into a file whose name is the current (unset) value of $JAVA_HOME rather than assigning that path as the variable's value.

CExport Java_Home which java

'Export Java_Home which java' is invalid shell syntax because it omits the assignment operator and command substitution required to set the variable to the output of the 'which java' command.

DSet JAVA_HOME -which javaCorrect

The form 'set JAVA_HOME=`which java`' uses command substitution to capture the path returned by 'which java' and assign it to JAVA_HOME before the Java application starts. Because java is confirmed installed and 'java -version' succeeds, 'which java' returns a valid path, satisfying the 'JAVA_HOME is not set' requirement at the script level. This approach resolves the error without requiring a persistent system-wide environment change outside the script.

Concept tested: Setting JAVA_HOME via command substitution in Linux scripts

Source: https://docs.oracle.com/en/java/javase/17/install/installation-jdk-linux-platforms.html

Topics

#environment variables#JAVA_HOME#shell scripting#export

Community Discussion

No community discussion yet for this question.

Full XK0-004 Practice