我是否需要在java包声明中提供完整路径?

时间:2013-11-17 17:47:13

标签: java

我在包org.zookeer中声明了一个java类,就像这个

zookeeper@zookeeper-virtual-machine:~/zookeeper/org/zookeeper$ head /home/zookeeper/zookeeper/org/zookeeper/Worker.java
package org.zookeeper;

该类成功编译。

我想用它的完整路径运行它(如下所示) - 但是我找到了一个找不到类的错误。

zookeeper@zookeeper-virtual-machine:~/zookeeper/org/zookeeper$ java -cp .:zookeeper-3.4.5.jar:slf4j-1.7.5/slf4j-api-1.7.5.jar:slf4j-1.7.5/slf4j-simple-1.7.5.jar /home/zookeeper/zookeeper/org/zookeeper/Worker 1 127.0.0.1 600
Exception in thread "main" java.lang.NoClassDefFoundError: /home/zookeeper/zookeeper/org/zookeeper/Worker
Caused by: java.lang.ClassNotFoundException: .home.zookeeper.zookeeper.org.zookeeper.Worker
    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: /home/zookeeper/zookeeper/org/zookeeper/Worker. Program will exit.

我知道如果我在包org.zookeeper中有一个类,它需要在一个文件夹/ org / zookeeper中。但是如果我想从完整路径调用它(它将由另一个进程启动)我是否需要在包声明中包含路径中的每个目录?

1 个答案:

答案 0 :(得分:0)

首先:包名称和类必须用点分隔: org.zookeeper.Worker

第二步:将根文件夹添加到类路径中: / home / zookeeper / zookeeper:zookeep ...

完整命令:     java -cp /home/zookeeper/zookeeper:zookeeper-3.4.5.jar:slf4j-1.7.5/slf4j-api-1.7.5.jar:slf4j-1.7.5/slf4j-simple-1.7.5.jar org .zookeeper.Worker 1 127.0.0.1 600