zookeeper-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Churu Tang <ct...@rubiconproject.com>
Subject Re: How to run a simple Java Client
Date Wed, 29 Oct 2014 23:20:48 GMT
It works!

Thank you Cameron for your patient and detailed explain! Thank you Ibrahim!

~$ java -cp "./:zookeeper-3.4.6.jar:./lib/*" ZKClient
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
=========Create Node===========
WatchedEvent state:SyncConnected type:None path:null
=============Verify node created successfully===============
znode1
=========Modify data==========
========Verify node modified successfully=========
sharpxiajun130901
=======Delete node==========
==========Verify node deleted successfully============
Note status:null

Cheers,
Churu


On Oct 29, 2014, at 3:19 PM, Ibrahim El-sanosi (PGR) <i.s.el-sanosi@newcastle.ac.uk>
wrote:

> Hi Churu,
> 
> These are some examples of setting the class path in run time:
> 
> 1- java -cp target/lib/*:target/* edu.brown.cs.zkbenchmark.ZooKeeperBenchmark --conf
benchmark.conf
> 
> 2- java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
> 
> 3- javac -classpath ./log4j-1.2.16.jar:./slf4j-api-1.6.1.jar:./slf4j-log4j12-1.6.1.jar
LearnerLeaderACK/*.java
> java -cp .:./log4j-1.2.16.jar:./slf4j-api-1.6.1.jar:./slf4j-log4j12-1.6.1.jar LearnerLeaderACK/StartLeader
> 
> 
> Ibrahim
> 
> -----Original Message-----
> From: Churu Tang [mailto:ctang@rubiconproject.com] 
> Sent: Wednesday, October 29, 2014 08:46 م
> To: user@zookeeper.apache.org
> Subject: Re: How to run a simple Java Client
> 
> Hi Ben,
> 
> Thanks for you reply! I include the classpath, but it still throws the same error
> 
> ~$ java ZKClient -classpath zookeeper-3.4.6.jar 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.
> 
> Anything else you think I can do to make it right?
> 
> Cheers,
> Churu
> 
> On Oct 29, 2014, at 1:28 PM, Ben Hsu <ben.hsu@criticalmedia.com> wrote:
> 
>> 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