In Linux, Java environment variables are system-wide or user-specific settings that determine how the Java Virtual Machine (JVM) and Java applications behave on the system.
In Linux, Java environment variables are system-wide or user-specific settings that determine how the Java Virtual Machine (JVM) and Java applications behave on the system. They are used to configure various aspects of the Java runtime environment, such as the Java home directory, classpath, and heap size.
The likelihood is that you have stumbled across this article when trying to run a Java application as a user on a system which has no environment variables set. In this guide we will set the common environment variables; JRE_HOME and JAVA_HOME.
First of all it's best to try and locate where the JRE / Java directories are on your system. If you have mlocate installed you can update the indexed directories and files with:
updatedb
Then search for Java / JRE directories with this:
locate java
or
locate jre
or
locate jdk
If you do not have mlocate installed the next best option is to use find. We can locate Java / JRE using find with:
find / -type d -name '*jre*'
or
find / -type d -name '*java*'
or
find / -type d -name '*jdk*'
Once you have located your Java directory proceed to set the environment variables using the instructions in the next step.
To set JRE_HOME we can run the following:
JRE_HOME=/path/to/jre
Example:
JRE_HOME=/opt/jdk1.8.0_111/
To set JAVA_HOME we can run the following:
JAVA_HOME=/path/to/java
Example:
JAVA_HOME=/opt/jdk1.8.0_111/
Most of the time JRE_HOME and JAVA_HOME will be the same path unless you require a different Java version for each.
To check that both JRE_HOME and JAVA_HOME are set we can run the following:
echo $JRE_HOME
echo $JAVA_HOME
After running the command above you should get the paths that you set in the previous step.
To export the set variables so that they can be used within the environment we have to run this:
export JAVA_HOME
export JRE_HOME
Now you should be able to use the Java application you may have previously had issues with.