zookeeper-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ben Hsu <ben....@criticalmedia.com>
Subject Re: How to run a simple Java Client
Date Wed, 29 Oct 2014 20:28:00 GMT
Hi Churu

I believe you have to set your classpath when running java (not just when
running javac!)

On Wed, Oct 29, 2014 at 4:07 PM, Churu Tang <ctang@rubiconproject.com>
wrote:

> Hi,
>
> I try to run a simple Java Client, but it keep throwing an error. Would
> someone help me to figure out why?
> ~$ javac ZKClient.java -classpath zookeeper-3.4.6.jar
> ~$ java ZKClient
> Exception in thread "main" java.lang.NoClassDefFoundError:
> org/apache/zookeeper/Watcher
> Caused by: java.lang.ClassNotFoundException: org.apache.zookeeper.Watcher
>         at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
>         at java.security.AccessController.doPrivileged(Native Method)
>         at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
>         at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
>         at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
>         at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
> Could not find the main class: ZKClient. Program will exit.
>
> I am a beginner in Java and Zookeeper, sorry for such a simple question.
> Many thanks!
>
> Cheers,
> Churu
>
>
> ——————————————————————————ZKClient.java—————————————————————————————
> import java.io.IOException;
>
> import org.apache.zookeeper.CreateMode;
> import org.apache.zookeeper.KeeperException;
> import org.apache.zookeeper.WatchedEvent;
> import org.apache.zookeeper.Watcher;
> import org.apache.zookeeper.ZooDefs.Ids;
> import org.apache.zookeeper.ZooKeeper;
>
> public class ZKClient {
>
>     public static void main(String[] args) throws Exception{
>         Watcher wh = new Watcher(){
>             @Override
>             public void process(WatchedEvent event) {
>                 System.out.println(event.toString());
>             }
>         };
>         ZooKeeper zk = new ZooKeeper("localhost:2181",30000,wh);
>         System.out.println("=========Create Node===========");
>         zk.create("/ctang", "znode1".getBytes(), Ids.OPEN_ACL_UNSAFE,
> CreateMode.PERSISTENT);
>         System.err.println("=============Verify node created
> successfully===============");
>         System.out.println(new String(zk.getData("/ctang", false, null)));
>         System.out.println("=========Modify data==========");
>         zk.setData("/ctang", "sharpxiajun130901".getBytes(), -1);
>         System.out.println("========Verify node modified
> successfully=========");
>         System.out.println(new String(zk.getData("/ctang", false, null)));
>         System.out.println("=======Delete node==========");
>         zk.delete("/ctang", -1);
>         System.out.println("==========Verify node deleted
> successfully============");
>         System.out.println("Note status:" + zk.exists("/ctang", false));
>         zk.close();
>     }
>
> }
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message