Skip to content

How to find and set JAVA_HOME

This is a totally short post, but vital information for me. I always end up searching for this online and always forget how to do it.

So, in case you have java installed, you often need to set the environment variable JAVA_HOME such that programs can find the executable. As far as I know, this variable is never set automatically on Linux, and finding out where the hell your installation is located is always a unnecessary quest.

java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'

This outputs the path as known to Java itself. You see that it also suppresses errors. Now you just need to either put this into your ~/.profile or ~/.bashrc file, where it is loaded every time you start your computer, or execute it manually when you need the environment variable:

export JAVA_HOME=$(java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home' | grep -o '/.*')

Same method as above, only that another grep invocation is used to extract the path.

Now let’s bookmark this post, only to forget about it again and hopefully finding it the next time I need it.

Published inTips and Tricks

Be First to Comment

Leave a Reply

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