How Do You Troubleshoot the Groovy Error “JAVA_HOME is not defined correctly”?

Problem scenario
You try to run a Groovy command (e.g., groovy foobar.groovy), but you get this error:
"groovy: JAVA_HOME is not defined correctly, can not execute: /usr/lib/jvm/java-9-openjdk/bin/java"

What should you do?

Solution

  1. Set the JAVA_HOME variable. Run this command: which java
  2. Mentally eliminate the trailing "/bin/java" from the above command. If Java was not found, follow these directions to install Java.
  3. Draft this command but replace "foobar" with the results of step #2 (with the elimination of "/bin/java" from the end of step #1's results): export JAVA_HOME=foobar
  4. Run the drafted command from above.
  5. Try to use Groovy again.

If you want the solution to persist a reboot, we recommend rebooting the server and seeing if your original problem is reproducible. Then run a command like this but replace "foobar" with the results of step #2 (with the elimination of "/bin/java" from the end of step #1's results):

echo 'export JAVA_HOME=/foobar' >> ~/.bashrc

Leave a comment

Your email address will not be published. Required fields are marked *