0

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:你们中的一些人可能已经猜到了 classname 是demo.rmi.librarymanager.server.LibraryManagerServer

编辑#2:好的 - 感谢人们的建议,我认为这次我的语法是正确的,但我仍然得到ClassNotFoundException但在 common 包中的不同类上:

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"

我应该在 -cp 之后将c:/development/eclipse/workspace/Library-Manager-Common/bin路径(所在的位置demo.rmi.librarymanager.common.Library)一起添加为命令行参数吗?c:/development/eclipse/workspace/Library-Manager-Server/bin

感谢大家的帮助和耐心——我正在开发一个 RMI 应用程序,一切都很好,直到我使用RMI 插件在 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"

感谢大家谦虚的淋浴。

4

3 回答 3

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.

我想我看到了您的问题-“类”不是关键字;它是您必须替换它的完全限定的类名。

如果您的类文件在 bin 中,则命令可能如下所示:

java -cp bin myClass

所以也许

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

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

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

于 2009-12-27T23:20:13.127 回答
2

这可能会起作用:

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

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

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

  • 使用类的完全限定名(包括包)告诉 java.exe 启动哪个类
  • 将与包层次结构对应的目录层次结构所在的基本目录作为类路径给出
于 2009-12-27T23:21:07.930 回答
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 路径使用 / 作为分隔符,即使在 Windows 上,类路径也应指向包层次结构的根,并且要运行的类以相对于类路径的包符号给出。

于 2009-12-27T23:26:28.610 回答