JAVA新手警报:尝试运行Java类并获取ClassNotFoundException

时间:2009-12-27 23:02:34

标签: java cmd classnotfound

Java新手。我正在尝试从cmd行运行java主类并返回ClassNotFoundException

java -cp class c:\development\eclipse\workspace\Library-Manager-Server\bin\demo\rmi\librarymanager\server\LibraryManagerServer 

-Djava.security.policy="C:\Development\Eclipse\Workspace\Library-Manager-Server\security.policy" 

-Djava.rmi.server.codebase="file:/C:/Development/Eclipse/Workspace/Library-Manager-Server/bin/ file:/C:/Development/Eclipse/Workspace/Library-Manager-Common/bin/"

该类肯定存在于bin中(eclipse会将其丢弃)但由于某种原因(可能是微不足道的),当我在上面运行cmd行时无法找到它。

任何帮助表示赞赏!

编辑#1 :因为有些人可能已经猜到了类名为demo.rmi.librarymanager.server.LibraryManagerServer

编辑#2 :好的 - 感谢人们的建议我认为这次我的语法正确但我仍然得到ClassNotFoundException但是在普通包中的另一个类:< / p>

demo.rmi.librarymanager.common.Library

这是新的cmd行:

java -cp c:/development/eclipse/workspace/Library-Manager-Server/bin demo.rmi.librarymanager.server.LibraryManagerServer 

-Djava.security.policy="C:/Development/Eclipse/Workspace/Library-Manager-Server/security.policy" 

-Djava.rmi.server.codebase="file:/C:/Development/Eclipse/Workspace/Library-Manager-Server/bin file:/C:/Development/Eclipse/Workspace/Library-Manager-Common/bin"

我应该将c:/development/eclipse/workspace/Library-Manager-Common/bin路径(其中demo.rmi.librarymanager.common.Library生存)和c:/development/eclipse/workspace/Library-Manager-Server/bin一起添加到-cp之后作为命令行参数吗?

感谢大家的帮助和耐心 - 我正在研究RMI应用程序,一切都很好,直到我使用RMI plugin在Eclipse中工作,但现在我有点困难,因为我相当新的java(正如你从我的问题看到的那样!)。

编辑#3 :好的,我开始工作了:

java -cp c:/development/eclipse/workspace/Library-Manager-Server/bin;c:/development/eclipse/workspace/Library-Manager-Common/bin 

demo.rmi.librarymanager.server.LibraryManagerServer 

-Djava.security.policy="C:/Development/Eclipse/Workspace/Library-Manager-Server/security.policy" 

-Djava.rmi.server.codebase="file:/C:/Development/Eclipse/Workspace/Library-Manager-Server/bin file:/C:/Development/Eclipse/Workspace/Library-Manager-Common/bin"

感谢所有人的谦卑。

3 个答案:

答案 0 :(得分:3)

java命令行是:

Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)

where options include:
    -client       to select the "client" VM
    -server       to select the "server" VM
    -hotspot      is a synonym for the "client" VM  [deprecated]
                  The default VM is client.

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
                  A ; separated list of directories, JAR archives,
                  and ZIP archives to search for class files.

我想我看到了你的问题 - 'class'不是关键字;它是fullQualified类名,你必须用它替换它。

如果你的类文件在bin中,那么命令可能如下所示:

java -cp bin myClass

所以也许

java -cp bin demo.rmi.librarymanager.server.LibraryManagerServer

如果您在Eclipse项目的目录中。

请注意,Eclipse具有类所在的设置(可以覆盖)。我发现默认情况下,不同版本的Eclipse有不同的版本。所以我看到binclasses或(因为我正在使用maven)target/classes

答案 1 :(得分:2)

这可能会奏效:

java -cp c:\development\eclipse\workspace\Library-Manager-Server\bin\ demo.rmi.librarymanager.server.LibraryManagerServer 

这假定您要在包LibraryManagerServer中运行类demo.rmi.librarymanager.server

请注意,如果您在包中使用类(除了简短测试或学习代码之外,建议这样做),您必须

  • 使用类的完全限定名称(包括包)告诉java.exe要启动哪个类
  • 为类路径提供 base 目录,在该目录下,与包层次结构对应的目录层次结构存在

答案 2 :(得分:1)

从我的命令行我猜:

java -cp c:/development/eclipse/workspace/Library-Manager-Server/bin
-Djava.security.policy="C:/Development/Eclipse/Workspace/Library-Manager-Server/security.policy" 
-Djava.rmi.server.codebase="file:/C:/Development/Eclipse/Workspace/Library-Manager-Server/bin/ file:/C:/Development/Eclipse/Workspace/Library-Manager-Common/bin/"
demo.rmi.librarymanager.server.LibraryManagerServer 

Java路径使用/作为分隔符,即使在widnows上,类路径也应该指向包层次结构的根,并且要运行的类以相对于类路径的包符号给出。