很多開發者安裝zookeeper的時候,應該會發現到這么一個問題: JAVA_HOME is not set
好的!那么這個是什么意思呢?
就是說你的 JAVA_HOME 變量沒有設定
為什么會提示這個呢?
其實zookeeper在啟動服務端的時候會基於java環境啟動,所以在啟動的時候會檢測 jdk 是否安裝
而在我們開發者的入門過程中,都會設定一下 %JAVA_HOME%的系統變量。
在 zkservice啟動的時候,會找%JAVA_HOME%\bin\java.jar 進行java基礎環境的啟動。所以,如果沒有配置的話,就要配置:
如何配置:區分兩種系統(自行百度吧)
Linux: vim /etc/profile 文件修改后,檢查是否完成 java -version
window:變量添加后,檢查是否完成 java -version
好的!按理說完成以上步驟之后,就是已經完成了%JAVA_HOME%的配置。
針對於window8 系統配置完成之后,使用 -version都可以發現正常進行了安裝,但是啟動的時候依舊報錯!JAVA_HOME is not set
這就不能忍了!於是我們看看,zkService 啟動的時候,到底做了些什么?
1、啟動加載zkEvn文件,
2、啟動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的環境下,沒遇到過。大家可以手動試試,並讀懂執行代碼,就可以自己修改和編寫了。
感謝看官,如果有疑問大家一起討論,關注或者留言。
