elasticsearch之JAVA环境变量报错:could not find java; set JAVA_HOME or ensure java is in PATH

时间:2022-02-17 16:41:22

在以RPM包安装elasticsearch过程中出现报错JAVA环境的问题:

● elasticsearch.service - Elasticsearch
Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2019-01-14 11:27:16 CST; 5s ago
Docs: http://www.elastic.co
Process: 2035 ExecStart=/usr/share/elasticsearch/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid --quiet (code=exited, status=1/FAILURE)
Main PID: 2035 (code=exited, status=1/FAILURE) Jan 14 11:27:16 ip-172-31-30-62.ec2.internal systemd[1]: Started Elasticsearch.
Jan 14 11:27:16 ip-172-31-30-62.ec2.internal systemd[1]: Starting Elasticsearch...
Jan 14 11:27:16 ip-172-31-30-62.ec2.internal elasticsearch[2035]: which: no java in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)
Jan 14 11:27:16 ip-172-31-30-62.ec2.internal elasticsearch[2035]: could not find java; set JAVA_HOME or ensure java is in PATH
Jan 14 11:27:16 ip-172-31-30-62.ec2.internal systemd[1]: elasticsearch.service: main process exited, code=exited, status=1/FAILURE
Jan 14 11:27:16 ip-172-31-30-62.ec2.internal systemd[1]: Unit elasticsearch.service entered failed state.
Jan 14 11:27:16 ip-172-31-30-62.ec2.internal systemd[1]: elasticsearch.service failed.

也就是说就算是你设置好JAVA全局环境变量后,elasticsearch启动的时候依然是在上面红色字体的PATH变量里面找java变量。这个不清楚究竟是什么原因。我们暂时把它当做是丢环境变量的原因吧。不过我们倒是有其他的解决方法:

在/etc/sysconfig/elasticsearch的这个文件里面设置JAVA_HOME环境变量:

################################
# Elasticsearch
################################ # Elasticsearch home directory
#ES_HOME=/usr/share/elasticsearch # Elasticsearch Java path
JAVA_HOME=/usr/local/jdk # Elasticsearch configuration directory
ES_PATH_CONF=/etc/elasticsearch # Elasticsearch PID directory
#PID_DIR=/var/run/elasticsearch

就是上面背景为青色的字体,设置一下环境变量后就可以正常启动了。

有时候也是编译环境的问题,比如上面这个问题就是,JAVA的jdk是源码安装的,而elasticsearch是yum一键安装的,因此很容易出现问题,所以我建议两者的安装尽量一致,jdk也使用yum安装比较好,这个问题就不会出现了。