gpt4 book ai didi

java - 为什么java找不到我的类?

转载 作者:行者123 更新时间:2023-12-01 07:28:56 26 4
gpt4 key购买 nike

我有一个编译没有错误的类。该类有一个 main 方法。但是当我尝试在 ubuntu linux 上从类目录运行它时,我收到一个类未找到错误。我很确定我错过了一些非常明显的东西,但我没有看到它。

这是我的 ls 操作:

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

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

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

最佳答案

最初的问题是您的类位于包中,但您试图加载它,就好像它不在包中一样。您通常会组织源代码以匹配您的包层次结构,然后从层次结构的运行类似以下内容的代码:

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

现在您已经通过将代码移出包来暂时解决了包问题,下一个问题是当前目录不在类路径中。所以代替这个:

java -cp "zookeeper-3.4.5.jar" LsGroup

你想要:

java -cp .:zookeeper-3.4.5.jar LsGroup

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

关于java - 为什么java找不到我的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19933918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com