传递命令行参数时找不到类

时间:2013-11-12 19:29:36

标签: java apache-zookeeper

我有一个Java类,其主要方法需要参数。如果我运行没有参数的类,我会得到这样的错误,这意味着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();
    }
}

3 个答案:

答案 0 :(得分:7)

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

lsGroup.connect(args[0]);

会因您看到的ArrayIndexOutOfBoundsException而失败。因此,ZooKeeper类永远不会被初始化,并且JVM永远不会解析slf4j的依赖性。

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

答案 1 :(得分:0)

java classpath缺少slf4j实现(我猜是slf4j.jar)。

这个jar是否捆绑在你的JAR中? 或者这个JAR在类路径的某个地方?

您需要提供所有类的依赖项才能运行它

答案 2 :(得分:0)

将所有api jar包装在一个目录中

在linux env上使用以下选项

  

java -cp'PATH_TO_DIR / *'className