zookeeper报错 JAVA_HOME is not set

不少开发者安装zookeeper的时候,应该会发现到这么一个问题: JAVA_HOME is not set html

好的!那么这个是什么意思呢?java

就是说你的  JAVA_HOME 变量没有设定express

为何会提示这个呢?apache

其实zookeeper在启动服务端的时候会基于java环境启动,因此在启动的时候会检测 jdk 是否安装vim

而在咱们开发者的入门过程当中,都会设定一下 %JAVA_HOME%的系统变量。windows

在 zkservice启动的时候,会找%JAVA_HOME%\bin\java.jar 进行java基础环境的启动。因此,若是没有配置的话,就要配置:app

如何配置:区分两种系统(自行百度吧)less

  Linux: vim /etc/profile 文件修改后,检查是否完成 java  -versionui

  window:变量添加后,检查是否完成 java -versionthis

好的!按理说完成以上步骤以后,就是已经完成了%JAVA_HOME%的配置。

针对于window8 系统配置完成以后,使用 -version均可以发现正常进行了安装,可是启动的时候依旧报错!JAVA_HOME is not set 

这就不能忍了!因而咱们看看,zkService 启动的时候,到底作了些什么?

  一、启动加载zkEvn文件,

  二、启动zkService文件,

也就是说,在zkEvn 文件里面可能有JAVA_HOME 的验证,因而咱们进去看看

@echo off
REM Licensed to the Apache Software Foundation (ASF) under one or more REM contributor license agreements. See the NOTICE file distributed with REM this work for additional information regarding copyright ownership. REM The ASF licenses this file to You under the Apache License, Version 2.0 REM (the "License"); you may not use this file except in compliance with REM the License. You may obtain a copy of the License at REM REM http://www.apache.org/licenses/LICENSE-2.0 REM REM Unless required by applicable law or agreed to in writing, software REM distributed under the License is distributed on an "AS IS" BASIS, REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. REM See the License for the specific language governing permissions and REM limitations under the License.

set ZOOCFGDIR=%~dp0%..\conf set ZOO_LOG_DIR=%~dp0%..\logs set ZOO_LOG4J_PROP=INFO,CONSOLE REM for sanity sake assume Java 1.6 REM see: http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html

REM add the zoocfg dir to classpath
set CLASSPATH=%ZOOCFGDIR% REM make it work in the release
SET CLASSPATH=%~dp0..\*;%~dp0..\lib\*;%CLASSPATH% REM make it work for developers
SET CLASSPATH=%~dp0..\build\classes;%~dp0..\build\lib\*;%CLASSPATH% set ZOOCFG=%ZOOCFGDIR%\zoo.cfg @REM setup java environment variables
 if not defined JAVA_HOME ( echo Error: JAVA_HOME is not set. goto :eof ) if not exist %JAVA_HOME%\bin\java.exe ( echo Error: JAVA_HOME is incorrectly set. goto :eof ) set JAVA=%JAVA_HOME%\bin\java

果真!这里有校验!并且校验的时候确定是不存在的,因此输出错误信息:JAVA_HOME is not set.

 

那么如何解决呢?

既然从系统变量获取获取不到这个变量,那么咱们干脆手动设置一下试试?

@echo off
REM Licensed to the Apache Software Foundation (ASF) under one or more REM contributor license agreements. See the NOTICE file distributed with REM this work for additional information regarding copyright ownership. REM The ASF licenses this file to You under the Apache License, Version 2.0 REM (the "License"); you may not use this file except in compliance with REM the License. You may obtain a copy of the License at REM REM http://www.apache.org/licenses/LICENSE-2.0 REM REM Unless required by applicable law or agreed to in writing, software REM distributed under the License is distributed on an "AS IS" BASIS, REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. REM See the License for the specific language governing permissions and REM limitations under the License.

REM for sanity sake assume Java 1.6 REM see: http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html

REM add the zoocfg dir to classpath
set CLASSPATH=%ZOOCFGDIR% REM make it work in the release
SET CLASSPATH=%~dp0..\*;%~dp0..\lib\*;%CLASSPATH% REM make it work for developers
SET CLASSPATH=%~dp0..\build\classes;%~dp0..\build\lib\*;%CLASSPATH% set JAVA=D:\java\jdk1.8.0_77\bin\java set JAVA_HOME=D:\java\jdk1.8.0_77 
set ZOOCFG=%ZOOCFGDIR%\zoo.cfg set ZOOCFGDIR=%~dp0%..\conf set ZOO_LOG_DIR=%~dp0%..\logs set ZOO_LOG4J_PROP=INFO,CONSOLE @REM setup java environment variables

if not defined JAVA_HOME ( echo Error: JAVA_HOME is not set. goto :eof ) if not defined JAVA ( echo Error: ----"%JAVA_HOME%"--- is set.but not found JAVA goto :eof )

修改内容如上:

手动设置JAVAHOME 和JAVA 的值,为了查找问题,在判断JAVA的时候,进行JAVAHOME 的值的打印,看看是否是真的设置成功了。

再次启动,果真!成功了!

 

好的,我们回顾一下。zkService 启动依赖java的环境,因此必需要可以启动java环境,对应的就是 JDK 安装目录下\bin\java.exe 须要被启动。

因此我们要告诉JAVA的值

也就是设置: 

set JAVA=D:\java\jdk1.8.0_77\bin\java (D:\java\jdk1.8.0_77 这里是你的JDK安装路径
再思考一点,我们设定
set JAVA_HOME=D:\java\jdk1.8.0_77 
目的其实就是让JAVA可以使用JAVA_HOME的变量的值,因此,既然我们都手动设定了JAVA的绝对路径,那么其实JAVA_HOME 的设置和判断均可以去掉了
对应简化内容为:

@echo off
REM Licensed to the Apache Software Foundation (ASF) under one or more REM contributor license agreements. See the NOTICE file distributed with REM this work for additional information regarding copyright ownership. REM The ASF licenses this file to You under the Apache License, Version 2.0 REM (the "License"); you may not use this file except in compliance with REM the License. You may obtain a copy of the License at REM REM http://www.apache.org/licenses/LICENSE-2.0 REM REM Unless required by applicable law or agreed to in writing, software REM distributed under the License is distributed on an "AS IS" BASIS, REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. REM See the License for the specific language governing permissions and REM limitations under the License.

REM for sanity sake assume Java 1.6 REM see: http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html

REM add the zoocfg dir to classpath
set CLASSPATH=%ZOOCFGDIR% REM make it work in the release
SET CLASSPATH=%~dp0..\*;%~dp0..\lib\*;%CLASSPATH% REM make it work for developers
SET CLASSPATH=%~dp0..\build\classes;%~dp0..\build\lib\*;%CLASSPATH% set JAVA=D:\java\jdk1.8.0_77\bin\java set ZOOCFG=%ZOOCFGDIR%\zoo.cfg set ZOOCFGDIR=%~dp0%..\conf set ZOO_LOG_DIR=%~dp0%..\logs set ZOO_LOG4J_PROP=INFO,CONSOLE @REM setup java environment variables


if not defined JAVA ( echo Error: not found JAVA goto :eof )

 

总结:这种状况目前只出如今window 8 的系统上,推测window 10 可能也会存在。可是在Linux的环境下,没遇到过。你们能够手动试试,并读懂执行代码,就能够本身修改和编写了。感谢看官,若是有疑问你们一块儿讨论,关注或者留言。