JAVA 新手警报:尝试运行 Java 类并收到 ClassNotFoundException

发布于 2024-08-16 07:58:03 字数 2276 浏览 7 评论 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将其删除的位置),但由于某种原因(可能是微不足道的)它无法找到当我运行上面的命令行时。

任何帮助表示赞赏!

EDIT#1:你们中的一些人可能已经猜到类名是 demo.rmi.librarymanager.server.LibraryManagerServer

EDIT#2:好的 - 谢谢对于人们的建议,我认为这次我的语法是正确的,但我仍然收到 ClassNotFoundException 但在公共包中的不同类上:

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 -cp 之后的 /workspace/Library-Manager-Server/bin 作为命令行参数?

感谢大家的帮助和耐心 - 我正在开发一个 RMI 应用程序,一切都很好,直到我使用 RMI 插件 但现在我遇到了一些困难,因为我对 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"

感谢大家的谦逊。

Java newbie. I am trying to run a java main class from cmd line and getting back 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/"

The class is definitely there in the bin (where eclipse drops it) but for some reason (probably trivial) it cannot be found when I run the cmd line above.

Any help appreciated!

EDIT#1: as some of you may have guessed classname is demo.rmi.librarymanager.server.LibraryManagerServer

EDIT#2: OK - thanks to people suggestions I think I got the syntax right this time but I am still getting ClassNotFoundException but on a different class in the common package:

demo.rmi.librarymanager.common.Library

Here's the new cmd line:

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"

Shall I add the c:/development/eclipse/workspace/Library-Manager-Common/bin path (where demo.rmi.librarymanager.common.Library lives) together with c:/development/eclipse/workspace/Library-Manager-Server/bin after -cp as a command line argument?

Thanks to everyone for their help and patience - I am working on an RMI application and everything was good till I was working inside eclipse using the RMI plugin but now I am having a bit of an hard time as I am fairly new to java (as you cas see from my question!).

EDIT#3: Ok I got it working:

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"

Thanks to everyone for the shower of humbleness.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

聽兲甴掵 2024-08-23 07:58:03

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' 不是关键字;它是您必须替换的完全限定的类名。

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

java -cp bin myClass

命令可能如下所示

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

如果您位于 Eclipse 项目的目录中,那么

。请注意,Eclipse 有类所在位置的设置(您可以覆盖这些设置)。我发现不同版本的Eclipse默认有不同的。所以我看到了 binclasses 或(因为我使用的是 maven)target/classes

The java commandline is:

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.

I think I see your problem - 'class' is not a keyword; it is the fullyQualified classname that you have to replace it with.

If your class files are in bin then the command might look like:

java -cp bin myClass

so perhaps

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

if you are in the directory of your Eclipse project.

Note that Eclipse has settings (which you can override) for where the classes are. I have found that different versions of Eclipse have different ones by default. So I have seen bin, classes or (as I am using maven) target/classes.

怎樣才叫好 2024-08-23 07:58:03

这可能会起作用:

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

假设您要运行 demo.rmi.librarymanager.server 包中的 LibraryManagerServer 类。

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

  • 使用类的完全限定名称(包括包)来告诉 java.exe 要启动哪个类
  • 将 < em>base 目录,与包层次结构对应的目录层次结构位于该目录下

This will probably work:

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

This assumes that you want to run the class LibraryManagerServer in the package demo.rmi.librarymanager.server.

Note that if you use classes in packages (which is advisable except for short test or learning code), you have to

  • Use the fully qualified name (including package) of the class to tell java.exe what class to start
  • Give as classpath the base directory under which the directory hierarchy corresponding to the package hierarchy lives
烟沫凡尘 2024-08-23 07:58:03

从你的命令行我猜想:

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上,类路径也应该指向包层次结构的根,并且要运行的类以相对于类路径的包表示法给出。

From your commandline I would guess:

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 paths use / as separators, even on widnows, the classpath should point to the root of your package hierarchy and the class to run is given in package notation relative to the classpath.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文