0

I have a class that compiles without error. The class has a main method. But when I try to run it on ubuntu linux from the classes' directory I get a class not found error. I am pretty sure I am missing something dead obvious but I don't see it.

Here is my ls operation:

zookeeper@zookeeper-virtual-machine:~/zookeeper-3.4.5/programs$ ls
CreateGroup.java  LsGroup.class  LsGroup.java  zookeeper-3.4.5.jar

Here is what happens when I to run LsGroup

zookeeper@zookeeper-virtual-machine:~/zookeeper-3.4.5/programs$ java -cp "zookeeper-3.4.5.jar" LsGroup
Exception in thread "main" java.lang.NoClassDefFoundError: LsGroup
Caused by: java.lang.ClassNotFoundException: LsGroup
    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: LsGroup. Program will exit.

Here is the code for LsGroup

package org.zookeeper;    
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.KeeperState;

public class LsGroup implements Watcher {

    private static final int SESSION_TIMEOUT = 5000;
    private ZooKeeper zk;
    private CountDownLatch connectedSignal = new CountDownLatch(1);

    public void connect(String hosts) throws IOException, InterruptedException {
        zk = new ZooKeeper(hosts, SESSION_TIMEOUT, this);
        connectedSignal.await();
    }

    @Override
    public void process(WatchedEvent event) { // Watcher interface
        if (event.getState() == KeeperState.SyncConnected) {
            connectedSignal.countDown();
        }
    }

    public void ls(String groupName) throws KeeperException, InterruptedException {
        String path = "/" + groupName;
        try {
            List<String> children = zk.getChildren(path, false);
            for (String child : children) {
                System.out.println(path+"/"+child);
                System.out.println(zk.getChildren(path +"/"+ child, false));
            }
        } catch (KeeperException.NoNodeException e) {
            System.out.printf("Group %s does not exist\n", groupName);
            System.exit(1);
        }
    }

    public void close() throws InterruptedException {
        zk.close();
    }

    public static void main(String[] args) throws Exception {
        LsGroup lsGroup = new LsGroup();
        lsGroup.connect(args[0]);
        lsGroup.ls(args[1]);
        lsGroup.close();
    }
}
bernie2436
  • 22,841
  • 49
  • 151
  • 244
  • 4
    possible duplicate of [NoClassDefFoundError: wrong name](http://stackoverflow.com/questions/7509295/noclassdeffounderror-wrong-name) – Jon Skeet Nov 12 '13 at 16:02
  • I deleted the package statement from the java class – bernie2436 Nov 12 '13 at 16:18
  • 1
    That's not really a great idea - it's good practice to use packages, but you should organize your source code to match your package structure, and know how to execute a class which is in a package. – Jon Skeet Nov 12 '13 at 16:22
  • @JonSkeet fair enough. That said, I'm still getting the error even when removing the package declaration. This means that something else is wrong as well. – bernie2436 Nov 12 '13 at 16:28
  • You've now changed the question substantially, so that all the original answers look crazy. That's not cool. (The problem is now that you haven't got the current directory in your classpath.) – Jon Skeet Nov 12 '13 at 16:35
  • @JonSkeet changing it back. Sorry – bernie2436 Nov 12 '13 at 16:36

7 Answers7

4

The original problem was that your class was in a package, but you were trying to load it as if it weren't in a package. You'd normally organize your source code to match your package hierarchy, then from the root of the hierarchy, you'd run something like:

java -cp .:zookeeper-3.4.5.jar org.zookeeper.LsGroup

Now that you've temporarily worked around the package issue by moving the code out of a package, the next problem is that the current directory isn't in the classpath. So instead of this:

java -cp "zookeeper-3.4.5.jar" LsGroup

You want:

java -cp .:zookeeper-3.4.5.jar LsGroup

Once you've got that working, you should move the classes back into packages, as per normal Java best practice.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • everything is working and I've just learned a bit about java packages/directories and stackoverflow decorum. Thanks for your help. – bernie2436 Nov 12 '13 at 16:43
2

You could remove the package declaration:

package org.zookeeper;

...or just place your LsGroup class in org/zookeeper directory.

Szymon Jednac
  • 2,969
  • 1
  • 29
  • 43
1

The class file is supposed to live in a path like:

 org/zookeeper/LsGroup.class

The -cp must include the directory that contains the org/ directory. Then you can

java -cp parent-of-org org.zookeeper.LsGroup
Ingo
  • 36,037
  • 5
  • 53
  • 100
1

The message "wrong name: org/zookeeper/LsGroup" means, you have to respect the package structure of Java. Use the following directory structure:

./org/zookeeper/LsGroup.class

Then launch java org.zookeeper.LsGroup from within the current directory. The package separator "." will be translated to corresponding directory.

Tires
  • 1,529
  • 16
  • 27
0

Your files are not under package org.zookeeper

You should be running your class from ~/zookeeper-3.4.5/org/zookeeper

Otherwise JVM won't find the classes to load.

Alex
  • 975
  • 10
  • 24
0

Your class is part of the org.zookeeper package but you keep it in the root folder of your project (/zookeeper-3.4.5/programs).
The qualified name of the package member and the path name to the file are parallel (see Managing Source and Class Files), so if your package is org.zookeeper the class file should be kept in /zookeeper-3.4.5/programs/org/zookeeper

Dror Bereznitsky
  • 20,048
  • 3
  • 48
  • 57
0

You made two mistakes:

1) You tried to run java LsGroup but you have to use the complete name including packages java org.zookeeper.LsGroup

2) your directory structure is not correct: the package org.zookeeper corresponds with the directory structure ./org/zookeeper/

If you change the directory structure and then run java from the top of this directory structure it should work

mschenk74
  • 3,561
  • 1
  • 21
  • 34