You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@zookeeper.apache.org by Churu Tang <ct...@rubiconproject.com> on 2014/10/29 21:07:39 UTC
How to run a simple Java Client
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();
}
}
Re: How to run a simple Java Client
Posted by Cameron McKenzie <mc...@gmail.com>.
Colon is the separate between classpath entries. When you added the
slf4j-log4j jar to the classpath you used a semi colon which is what has
caused the problem.
java -cp "./:zookeeper-3.4.6.jar:./lib/slf4j-log4j12-1.6.1.jar" ZKClient
ZK relies on a stack of libraries, so you're going to be going through this
process of adding jars to your classpath for a while. It's probably better
to write a wrapper script to run your client that just adds all of the jars
in the lib directory to the JVMs classpath and runs your ZKClient.
On Thu, Oct 30, 2014 at 9:00 AM, Churu Tang <ct...@rubiconproject.com>
wrote:
> Hi Cameron,
>
> Thank you for your explaining! I followed your guide then another error
> occurs.
>
> $ java -cp ./:zookeeper-3.4.6.jar ZKClient
> Exception in thread "main" java.lang.NoClassDefFoundError:
> org/slf4j/LoggerFactory
> at org.apache.zookeeper.ZooKeeper.<clinit>(ZooKeeper.java:94)
> at ZKClient.main(ZKClient.java:19)
> Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
> 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)
> ... 2 more
>
> My best guess is that I need to include the ./lib/slf4j-log4j12-1.6.1.jar.
> But again I am having the following java.lang.NoClassDefFoundError Watcher
> error, which I had before.
>
> $ java -cp "./:zookeeper-3.4.6.jar;./lib/slf4j-log4j12-1.6.1.jar" 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.
>
> Also, would you tell me why there is a colon “:” in
> "./:zookeeper-3.4.6.jar”? I try to google but unable to find it. Sorry for
> the naive question. Thanks!
>
>
> Cheers,
> Churu
>
> On Oct 29, 2014, at 2:31 PM, Cameron McKenzie <mc...@gmail.com>
> wrote:
>
> > java -cp ./:zookeeper-3.4.6.jar ZKClient
>
>
Re: How to run a simple Java Client
Posted by Churu Tang <ct...@rubiconproject.com>.
Hi Cameron,
Thank you for your explaining! I followed your guide then another error occurs.
$ java -cp ./:zookeeper-3.4.6.jar ZKClient
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.apache.zookeeper.ZooKeeper.<clinit>(ZooKeeper.java:94)
at ZKClient.main(ZKClient.java:19)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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)
... 2 more
My best guess is that I need to include the ./lib/slf4j-log4j12-1.6.1.jar. But again I am having the following java.lang.NoClassDefFoundError Watcher error, which I had before.
$ java -cp "./:zookeeper-3.4.6.jar;./lib/slf4j-log4j12-1.6.1.jar" 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.
Also, would you tell me why there is a colon “:” in "./:zookeeper-3.4.6.jar”? I try to google but unable to find it. Sorry for the naive question. Thanks!
Cheers,
Churu
On Oct 29, 2014, at 2:31 PM, Cameron McKenzie <mc...@gmail.com> wrote:
> java -cp ./:zookeeper-3.4.6.jar ZKClient
Re: How to run a simple Java Client
Posted by Cameron McKenzie <mc...@gmail.com>.
You need to specify the jar file on the classpath in addition to the
directory containing your classes.
java -cp ./:zookeeper-3.4.6.jar ZKClient
On Thu, Oct 30, 2014 at 8:27 AM, Churu Tang <ct...@rubiconproject.com>
wrote:
> Hi Steven,
>
> Thanks for your help!
> I just checked, all class file, java file and jar file are under the
> current directory.
> ~$ ls *.class *.jar *.java
> ZKClient$1.class ZKClient.class ZKClient.java zookeeper-3.4.6.jar
>
> However, it still has the same error.
> ~$ java -cp . 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 running on a GNU/Linux Environment, and using the stable version
> zookeeper-3.4.6. I am able to run the Java and C binding Clients that comes
> with the package. Do I need to install any other packages?
>
> Cheers,
> Churu
>
> On Oct 29, 2014, at 1:55 PM, Steven Siebert <sm...@gmail.com> wrote:
>
> >>> ~$ java ZKClient -classpath zookeeper-3.4.6.jar
> >
> > You're passing your classpath and jar file name in as arguments.
> >
> > Assuming your executing from directory that contains both the class file
> > you compiled and the zookeeper jar,try:
> >
> > java -cp . ZKClient
> >
> > On Wed, Oct 29, 2014 at 4:45 PM, Churu Tang <ct...@rubiconproject.com>
> > wrote:
> >
> >> 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 <be...@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 <ct...@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();
> >>>> }
> >>>>
> >>>> }
> >>>>
> >>>>
> >>
> >>
>
>
Re: How to run a simple Java Client
Posted by Churu Tang <ct...@rubiconproject.com>.
Hi Steven,
Thanks for your help!
I just checked, all class file, java file and jar file are under the current directory.
~$ ls *.class *.jar *.java
ZKClient$1.class ZKClient.class ZKClient.java zookeeper-3.4.6.jar
However, it still has the same error.
~$ java -cp . 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 running on a GNU/Linux Environment, and using the stable version zookeeper-3.4.6. I am able to run the Java and C binding Clients that comes with the package. Do I need to install any other packages?
Cheers,
Churu
On Oct 29, 2014, at 1:55 PM, Steven Siebert <sm...@gmail.com> wrote:
>>> ~$ java ZKClient -classpath zookeeper-3.4.6.jar
>
> You're passing your classpath and jar file name in as arguments.
>
> Assuming your executing from directory that contains both the class file
> you compiled and the zookeeper jar,try:
>
> java -cp . ZKClient
>
> On Wed, Oct 29, 2014 at 4:45 PM, Churu Tang <ct...@rubiconproject.com>
> wrote:
>
>> 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 <be...@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 <ct...@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();
>>>> }
>>>>
>>>> }
>>>>
>>>>
>>
>>
Re: How to run a simple Java Client
Posted by Steven Siebert <sm...@gmail.com>.
>> ~$ java ZKClient -classpath zookeeper-3.4.6.jar
You're passing your classpath and jar file name in as arguments.
Assuming your executing from directory that contains both the class file
you compiled and the zookeeper jar,try:
java -cp . ZKClient
On Wed, Oct 29, 2014 at 4:45 PM, Churu Tang <ct...@rubiconproject.com>
wrote:
> 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 <be...@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 <ct...@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();
> >> }
> >>
> >> }
> >>
> >>
>
>
Re: How to run a simple Java Client
Posted by Churu Tang <ct...@rubiconproject.com>.
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....@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 <be...@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 <ct...@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();
>>> }
>>>
>>> }
>>>
>>>
>
RE: How to run a simple Java Client
Posted by "Ibrahim El-sanosi (PGR)" <i....@newcastle.ac.uk>.
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 <be...@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 <ct...@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();
>> }
>>
>> }
>>
>>
Re: How to run a simple Java Client
Posted by Churu Tang <ct...@rubiconproject.com>.
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 <be...@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 <ct...@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();
>> }
>>
>> }
>>
>>
Re: How to run a simple Java Client
Posted by Ben Hsu <be...@criticalmedia.com>.
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 <ct...@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();
> }
>
> }
>
>