ZooKeeper啓動時報錯:JAVA_HOME is not set

情景再現:win7系統,jdk1.8,安裝完成後啓動Zookeeper

很明顯,就是說我得jdk路徑沒有設置,這個的確,jdk1.8已經不需要設置路徑了。java -version當然是存在的。
1.最直接的方式就是去環境變量中添加JAVA_HOME
2.另一種方式,在zkServer.cmd啓動的時候,會找%JAVA_HOME%\bin\java.jar 進行java基礎環境的啓動。所以,zkService啓動的時候肯定是對 JAVA_HOME進行了驗證。
zkServie啓動的時候:

  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%..
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
)

set JAVA_HOME=%JAVA_HOME:"=%

if not exist "%JAVA_HOME%"\bin\java.exe (
  echo Error: JAVA_HOME is incorrectly set.
  goto :eof
)

REM strip off trailing \ from JAVA_HOME or java does not start
if "%JAVA_HOME:~-1%" EQU "\" set "JAVA_HOME=%JAVA_HOME:~0,-1%"
 
set JAVA="%JAVA_HOME%"\bin\java

很明顯我們能看到對JAVA_HOME的判斷,所以我們可以在這裏把JAVA_HOME寫死。

只需要在

if not defined JAVA_HOME

前面添加

 set JAVA_HOME=D:\Program Files\Java\jdk1.8.0_151

然後運行 zkServie.cmd,成功運行!
在這裏插入圖片描述
爲了代碼的簡潔:

set JAVA_HOME=%JAVA_HOME:"=%

這裏以及之後的代碼都可以刪除。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章