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 <[email protected]> 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 <[email protected]> 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 <[email protected]> > > 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 <[email protected]> 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 <[email protected]> > >>> 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(); > >>>> } > >>>> > >>>> } > >>>> > >>>> > >> > >> > >
