You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@zookeeper.apache.org by kumuduharshani <ku...@gmail.com> on 2012/04/01 07:10:28 UTC

Simple Java class to talk to ZooKeeper

HI..

I am new to Zookeeper and tried reading many tutorials and got a basic
knowledge on it.. I need to an assignment with ZooKeeper, but before I was
just trying to do basic connection to zookeeper via a simple java class.
But, it giving a exception when connecting. This is how i did,,,

[On Windows]
Started ZooKeeper on terminal - zkServer.cmd (it is running now), verified
via zkCli.cmd too.

Written following simple java class:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author v-kusama
 */

import java.io.IOException;
import java.util.List;
import org.apache.zookeeper.*;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper.States;

public class zkConnector implements Watcher{
    
    static ZooKeeper zooKeeper ;

    static Integer mutex;
    
     java.util.concurrent.CountDownLatch connectedSignal = new
java.util.concurrent.CountDownLatch(1);
    
    String root;

    public void connect() throws KeeperException, IOException,
InterruptedException
    {
        zooKeeper = new ZooKeeper("localhost:2181", 3000, 
                new Watcher(){

                @Override
                public void process(WatchedEvent event) {
                    if (event.getState() == KeeperState.SyncConnected) {
                                connectedSignal.countDown();
                            }
                }
                    
                });
    }
    
    public static void main (String args[]) throws KeeperException,
IOException, InterruptedException
    {
        zkConnector zk = new zkConnector();
        zk.connect();
        
        String lock1 = zooKeeper.create("/locks", new byte[0],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("Done" + lock1);

        String lock2 = zooKeeper.create("/locks/lock", new byte[0],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
System.out.println("Done" + lock2);

        List children = zooKeeper.getChildren("/locks", false);
        for(Object child:children){
        System.out.println("child:" + child);
        } 
        
    }

    @Override
    public void process(WatchedEvent event) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}

#Code compiled well
#Run the class, but failed with an exception
run:
Exception in thread "main" java.lang.NoClassDefFoundError:
org/slf4j/LoggerFactory
	at org.apache.zookeeper.ZooKeeper.<clinit>(ZooKeeper.java:93)
	at zkConnector.connect(zkConnector.java:29)
	at zkConnector.main(zkConnector.java:45)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
	... 3 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)


#Exception points to following line in connect() method:
zooKeeper = new ZooKeeper("localhost:2181", 3000, 


Am i missing something.. how to get this done to talk to zookeeper server...


thanks in advance
Kumudu....


--
View this message in context: http://zookeeper-user.578899.n2.nabble.com/Simple-Java-class-to-talk-to-ZooKeeper-tp7426147p7426147.html
Sent from the zookeeper-user mailing list archive at Nabble.com.

Re: Simple Java class to talk to ZooKeeper

Posted by sunil singh <su...@yahoo.com>.
Hi all,
Thanks bro.. because of ur pgm i atleast i am able to connect to server and
fetch some data.



--
View this message in context: http://zookeeper-user.578899.n2.nabble.com/Simple-Java-class-to-talk-to-ZooKeeper-tp7426147p7577844.html
Sent from the zookeeper-user mailing list archive at Nabble.com.

Re: Simple Java class to talk to ZooKeeper

Posted by Sumedha Kodithuwakku <su...@gmail.com>.
Hi,
You have to download slf4j[1] libraries and add them to the class path.
Adding slf4j simple and api jars would be enough as I think.

[1] http://www.slf4j.org/download.html

On Sunday, April 1, 2012, kumuduharshani <ku...@gmail.com> wrote:
> HI..
>
> I am new to Zookeeper and tried reading many tutorials and got a basic
> knowledge on it.. I need to an assignment with ZooKeeper, but before I was
> just trying to do basic connection to zookeeper via a simple java class.
> But, it giving a exception when connecting. This is how i did,,,
>
> [On Windows]
> Started ZooKeeper on terminal - zkServer.cmd (it is running now), verified
> via zkCli.cmd too.
>
> Written following simple java class:
>
> /*
>  * To change this template, choose Tools | Templates
>  * and open the template in the editor.
>  */
>
> /**
>  *
>  * @author v-kusama
>  */
>
> import java.io.IOException;
> import java.util.List;
> import org.apache.zookeeper.*;
> import org.apache.zookeeper.Watcher.Event.KeeperState;
> import org.apache.zookeeper.ZooKeeper.States;
>
> public class zkConnector implements Watcher{
>
>    static ZooKeeper zooKeeper ;
>
>    static Integer mutex;
>
>     java.util.concurrent.CountDownLatch connectedSignal = new
> java.util.concurrent.CountDownLatch(1);
>
>    String root;
>
>    public void connect() throws KeeperException, IOException,
> InterruptedException
>    {
>        zooKeeper = new ZooKeeper("localhost:2181", 3000,
>                new Watcher(){
>
>                @Override
>                public void process(WatchedEvent event) {
>                    if (event.getState() == KeeperState.SyncConnected) {
>                                connectedSignal.countDown();
>                            }
>                }
>
>                });
>    }
>
>    public static void main (String args[]) throws KeeperException,
> IOException, InterruptedException
>    {
>        zkConnector zk = new zkConnector();
>        zk.connect();
>
>        String lock1 = zooKeeper.create("/locks", new byte[0],
> ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
>        System.out.println("Done" + lock1);
>
>        String lock2 = zooKeeper.create("/locks/lock", new byte[0],
> ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
> System.out.println("Done" + lock2);
>
>        List children = zooKeeper.getChildren("/locks", false);
>        for(Object child:children){
>        System.out.println("child:" + child);
>        }
>
>    }
>
>    @Override
>    public void process(WatchedEvent event) {
>        throw new UnsupportedOperationException("Not supported yet.");
>    }
>
> }
>
> #Code compiled well
> #Run the class, but failed with an exception
> run:
> Exception in thread "main" java.lang.NoClassDefFoundError:
> org/slf4j/LoggerFactory
>        at org.apache.zookeeper.ZooKeeper.<clinit>(ZooKeeper.java:93)
>        at zkConnector.connect(zkConnector.java:29)
>        at zkConnector.main(zkConnector.java:45)
> Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
>        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
>        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
>        at java.security.AccessController.doPrivileged(Native Method)
>        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
>        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
>        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
>        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
>        ... 3 more
> Java Result: 1
> BUILD SUCCESSFUL (total time: 1 second)
>
>
> #Exception points to following line in connect() method:
> zooKeeper = new ZooKeeper("localhost:2181", 3000,
>
>
> Am i missing something.. how to get this done to talk to zookeeper
server...
>
>
> thanks in advance
> Kumudu....
>
>
> --
> View this message in context:
http://zookeeper-user.578899.n2.nabble.com/Simple-Java-class-to-talk-to-ZooKeeper-tp7426147p7426147.html
> Sent from the zookeeper-user mailing list archive at Nabble.com.
>

-- 
Sumedha Sanjeewa
Undergraduate
Department Of Computer Science & Engineering,
University Of Moratuwa,
Sri Lanka.