天天看點

解決zookeeper linux下無法啟動的問題

在linux下安裝zookeeper時,出現了如下的錯誤:

[[email protected] bin]$ ./zkServer.sh start
JMX disabled by user request
Using config: /opt/zookeeper-3.3.3/bin/../conf/zoo.cfg
Starting zookeeper ... 
STARTED
[[email protected] bin]$ 2011-07-05 10:33:57,587 - INFO  [main:[email protected]] - Reading configuration from: /opt/zookeeper-3.3.3/bin/../conf/zoo.cfg
2011-07-05 10:33:57,596 - WARN  [main:[email protected]] - Either no config or no quorum defined in config, running  in standalone mode
Exception in thread "main" java.lang.NoSuchMethodError: method java.lang.management.ManagementFactory.getPlatformMBeanServer with signature ()Ljavax.management.MBeanServer; was not found.
   at org.apache.zookeeper.jmx.ManagedUtil.registerLog4jMBeans(ManagedUtil.java:48)
   at org.apache.zookeeper.server.ZooKeeperServerMain.initializeAndRun(ZooKeeperServerMain.java:73)
   at org.apache.zookeeper.server.ZooKeeperServerMain.main(ZooKeeperServerMain.java:51)
   at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:108)
   at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:76)
           

 發現沒有找到java.lang.management下的一個方法。如果使用的是标準的JDK的話,不應該出現這樣的錯誤的。于是檢視zkServer.sh的腳本。發現啟動java程式的腳本如下:

java  "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
    -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" &      

 在linux下直接執行java指令,發現輸出如下:

[[email protected] bin]$ java
Usage: gij [OPTION] ... CLASS [ARGS] ...
          to invoke CLASS.main, or
       gij -jar [OPTION] ... JARFILE [ARGS] ...
          to execute a jar file
Try `gij --help' for more information.      

 顯然,zkServer.sh使用的java不正确。使用的gij的java。解決這個問題就簡單了,把腳本改成:

$JAVA_HOME/bin/java  "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
    -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" &      

這樣修改之後,就能正常運作了。