为什么java找不到我的类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19933918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:39:06  来源:igfitidea点击:

Why can't java find my class?

java

提问by bernie2436

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.

我有一个编译没有错误的类。该类有一个主要方法。但是当我尝试从类的目录中在 ubuntu linux 上运行它时,我得到一个找不到类的错误。我很确定我遗漏了一些明显的东西,但我没有看到。

Here is my ls operation:

这是我的 ls 操作:

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

这是我运行 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.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

这是 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();
    }
}

采纳答案by Jon Skeet

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 rootof 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.

一旦你开始工作,你应该按照正常的 Java 最佳实践将类移回包中。

回答by Ingo

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

-cp 必须包括包含 org/ 目录的目录。然后你可以

java -cp parent-of-org org.zookeeper.LsGroup

回答by Szymon Jednac

You could remove the package declaration:

您可以删除包声明:

package org.zookeeper;

...or just place your LsGroupclass in org/zookeeperdirectory.

...或者只是将您的LsGroup课程放在org/zookeeper目录中。

回答by Tires

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”意味着,您必须尊重 Java 的包结构。使用以下目录结构:

./org/zookeeper/LsGroup.class

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

然后从当前目录中启动 java org.zookeeper.LsGroup。包分隔符“.” 将被翻译到相应的目录。

回答by Alex

Your files are not under package org.zookeeper

您的文件不在包 org.zookeeper 下

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

你应该从 ~/zookeeper-3.4.5/org/zookeeper 运行你的课程

Otherwise JVM won't find the classes to load.

否则 JVM 将找不到要加载的类。

回答by Dror Bereznitsky

Your class is part of the org.zookeeperpackage 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.zookeeperthe class file should be kept in /zookeeper-3.4.5/programs/org/zookeeper

您的类是org.zookeeper包的一部分,但您将其保存在项目的根文件夹中 ( /zookeeper-3.4.5/programs)。
包成员的限定名和文件的路径名是平行的(参见管理源文件和类文件),所以如果你的包是org.zookeeper类文件应该保存在/zookeeper-3.4.5/programs/org/zookeeper

回答by mschenk74

You made two mistakes:

你犯了两个错误:

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

1)您尝试运行,java LsGroup但必须使用包括包在内的完整名称java org.zookeeper.LsGroup

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

2)你的目录结构不正确:包org.zookeeper对应的目录结构./org/zookeeper/

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

如果您更改目录结构,然后从该目录结构的顶部运行 java 它应该可以工作