gpt4 book ai didi

java - 当我传递命令行参数时找不到类

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

我有一个 Java 类,它的 main 方法需要参数。如果我运行不带参数的类,我会收到这样的错误,这意味着 Java 找到该类,运行它,并生成运行时 ArrayIndexOutOfBoundsException。

zookeeper@zookeeper-virtual-machine:~/zookeeper-3.4.5$ java -cp .:zookeeper-3.4.5.jar org.zookeeper.LsGroup
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at org.zookeeper.LsGroup.main(LsGroup.java:50)

但是当我使用必要的命令行参数尝试相同的语句( zookeeper@zookeeper-virtual-machine:~/zookeeper-3.4.5$ java -cp .:zookeeper-3.4.5.jar org.zookeeper.LsGroup Test )时,我收到一个找不到类的错误:

zookeeper@zookeeper-virtual-machine:~/zookeeper-3.4.5$ java -cp .:zookeeper-3.4.5.jar org.zookeeper.LsGroup Test
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.apache.zookeeper.ZooKeeper.<clinit>(ZooKeeper.java:94)
at org.zookeeper.LsGroup.connect(LsGroup.java:19)
at org.zookeeper.LsGroup.main(LsGroup.java:50)
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)
... 3 more
zookeeper@zookeeper-virtual-machine:~/zookeeper-3.4.5$

今天早上我遇到了类似的问题,但该线程上的特定问题已解决。 Why can't java find my class?

为什么当我传递命令行参数时会出现类未找到错误?我该如何解决这个问题?

这是java代码:

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

最佳答案

您的 ZooKeeper 类似乎依赖于 slf4j。如果您没有指定任何命令行参数,则此行

lsGroup.connect(args[0]);

将会失败,并出现您看到的ArrayIndexOutOfBoundsException。因此,ZooKeeper 类永远不会被初始化,并且 JVM 永远不会解析对 slf4j 的依赖关系。

如果您指定了命令行参数。 JVM 将尝试加载您的类,但会失败,因为 slf4j 不在类路径上。您需要将相关的 jar 添加到您的类路径中。

关于java - 当我传递命令行参数时找不到类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19938191/

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