patrickbarker patrickbarker - 10 months ago 47
Java Question

Java not found by Elasticsearch on Centos 6.6, all path variables are set and working

I recently spun up a vagrant server and wanted to get Elasticsearch going on it. So, I installed Oracle Java and ES on a "chef/Centos-6.6" vagrant cloud VM. I set my Java path using a shell script in "etc/profile.d".

Here is my provisioning script:

#!/usr/bin/env bash
yum -y update

wget -O /opt/jdk-7u67-linux-x64.tar.gz --no-cookies --no-check-certificate --header "Cookie:; oraclelicense=accept-securebackup-cookie" ""
tar xzf /opt/jdk-7u67-linux-x64.tar.gz -C /opt/
touch /etc/profile.d/
echo "export JAVA_HOME=/opt/jdk1.7.0_67" >> /etc/profile.d/
echo "export JRE_HOME=/opt/jdk1.7.0_67/jre" >> /etc/profile.d/
echo "export PATH=$PATH:/opt/jdk1.7.0_67/bin:/opt/jdk1.7.0_67/jre/bin" >> /etc/profile.d/

rpm --import
name=Elasticsearch repository for 1.3.x packages
echo "$REPO" > /etc/yum.repos.d/elasticsearch.repo
yum install -y elasticsearch

The install all goes fine. However, when I run "sudo service elasticsearch start" I get:

which: no java in (/sbin:/usr/sbin:/bin:/usr/bin)

but if I "echo $PATH" for home user i get:


and for root user $PATH i get:


and "echo $JAVA_HOME" i get:


if I run "which java" i get:


and if I run "java" it shows up with the man page.

How is it that elasticsearch is not looking in my path for java? Why is it only looking in the default Centos path? What am i missing here?


The source of your problem is probably that init scripts (Elastic init script in this case) don't see most environment variables (eg JAVA_HOME, JRE_HOME, etc).

If you look at the ElasticSearch init script, you will see that PATH variable is set explicitly in that init script and JAVA_HOME is determined by looping through predefined set of possible locations:

JDK_DIRS="/usr/lib/jvm/jdk-7-oracle-x64 /usr/lib/jvm/java-7-oracle /usr/lib/jvm/java-7-openjdk /usr/lib/jvm/java-7-openjdk-amd64/ /usr/lib/jvm/java-7-openjdk-armhf /usr/lib/jvm/java-7-openjdk-i386/ /usr/lib/jvm/default-java"

So you could put your Java installation for example to /usr/lib/jvm/jdk-7-oracle-x64 directory and the init script should pick it up.


Looking at the init script I noticed that you can set JAVA_HOME in /etc/default/elasticsearch to skip looping through the predefined JDK locations as mentioned above (Source).