When you install Java via yum install java
on CentOS, the system typically creates symlinks in /usr/bin
, but the actual Java installation resides elsewhere. The JAVA_HOME
environment variable should point to the root directory of your JDK/JRE installation, not to the binary symlink.
First, let's find where Java is actually installed:
# For OpenJDK installations:
sudo alternatives --config java
# Or use this command to follow symlinks:
readlink -f $(which java)
Example output might show: /usr/lib/jvm/java-11-openjdk-11.0.19.0.7-1.el7_9.x86_64/bin/java
The JAVA_HOME
should be the parent directory of the bin
folder containing the java executable. From the above example:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.19.0.7-1.el7_9.x86_64
To make this persistent across reboots:
Option 1: System-wide configuration (requires root)
echo "export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.19.0.7-1.el7_9.x86_64" >> /etc/profile.d/java.sh
chmod +x /etc/profile.d/java.sh
Option 2: User-specific configuration
echo "export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.19.0.7-1.el7_9.x86_64" >> ~/.bashrc
source ~/.bashrc
After setting the variable, verify it works correctly:
echo $JAVA_HOME
$JAVA_HOME/bin/java -version
For more complex environments, you might want an automated way to set JAVA_HOME:
#!/bin/bash
# Find Java home automatically
if type -p java &> /dev/null; then
JAVA_EXEC=$(readlink -f $(which java))
if [[ "$JAVA_EXEC" == */bin/java ]]; then
export JAVA_HOME=${JAVA_EXEC%/bin/java}
echo "Discovered JAVA_HOME: $JAVA_HOME"
else
echo "Could not determine JAVA_HOME from java executable path"
fi
else
echo "Java not found in PATH"
fi
- Never set JAVA_HOME to /usr/bin (this is just symlinks)
- For multiple Java versions, use
alternatives
to manage them - Some applications expect trailing slash in JAVA_HOME, others don't
When you install Java via yum
on CentOS, the system places the Java binaries in standard paths like /usr/bin/java
, but this isn't where JAVA_HOME should point. JAVA_HOME must reference the JDK/JRE installation directory containing bin
, lib
, and other critical folders.
First, identify where Java is truly installed:
readlink -f $(which java)
# Typical output for OpenJDK: /usr/lib/jvm/java-11-openjdk-11.0.19.0.7-1.el7_9.x86_64/bin/java
Strip the trailing /bin/java
to get your JAVA_HOME:
dirname $(dirname $(readlink -f $(which java)))
For permanent configuration, add this to ~/.bashrc
or /etc/profile
:
export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
export PATH=$PATH:$JAVA_HOME/bin
Check your configuration with:
echo $JAVA_HOME
java -version
javac -version # If JDK is installed
For multi-Java environments, use alternatives
:
sudo alternatives --config java
Here's a robust bash function for Java environment setup:
setup_java_env() {
local java_path=$(readlink -f $(which java))
if [[ -z "$java_path" ]]; then
echo "Java not found!" >&2
return 1
fi
export JAVA_HOME=$(dirname $(dirname "$java_path"))
export PATH=$PATH:$JAVA_HOME/bin
cat <> ~/.bashrc
# Java Environment
export JAVA_HOME=$JAVA_HOME
export PATH=\$PATH:\$JAVA_HOME/bin
EOF
echo "Java environment configured for $JAVA_HOME"
}
When managing several Java installations, consider this pattern:
# In /etc/profile.d/java.sh
export JAVA_8_HOME=$(dirname $(dirname $(readlink -f $(which java))))
export JAVA_11_HOME=/usr/lib/jvm/java-11-openjdk
# Default to Java 11
export JAVA_HOME=$JAVA_11_HOME