【问题标题】:java - NoClassDefFoundError but class is therejava - NoClassDefFoundError 但类在那里
【发布时间】:2023-01-16 00:32:31
【问题描述】:

我用 intelliJ 开发了一个 Java 项目,现在我想通过命令行运行我的程序。所以我转到 project/out/production/project 文件夹,我的 *.class 文件所在的位置并尝试运行我的文件“java FileCopy [args]”(FileCopy.class 在那里,里面的类具有相同的名称)但是我收到错误消息“Fehler:无法找到或加载主类 FileCopy Ursache:java.lang.NoClassDefFoundError:BelegFileTransfer/FileCopy(错误名称:FileCopy)”

如果我想用 intelliJ 运行我的项目,没问题。

我究竟做错了什么?

我的目录结构:

FileTransfer
-> .idea
-> out
--> production
----> FileTransfer (thats where the .class files located and i am running the cmd)
-> src
--> FileTransfer (thats where the .java files located)

错误: [1]:https://i.stack.imgur.com/Ft2q0.png

【问题讨论】:

  • 很难知道您的文件在哪里以及存在哪些包。请发帖(格式化为代码) 输出以下内容,已更改为目录 BelegDateitransferNeu(我无法正确发布路径,因为你发布了一张图片 - 请学习从 cmd.exe 复制粘贴)。命令dir /s/b

标签: java noclassdeffounderror


【解决方案1】:

看起来问题与类路径有关。当您从命令行运行该程序时,Java 运行时无法找到主类“FileCopy”,因为它不在类路径中。

要解决此问题,您可以:

  1. 运行程序时指定类的完整包名:“java FileTransfer.FileCopy [args]”
  2. 运行程序时将“out/production”目录添加到类路径:“java -cp out/production FileCopy [args]”

    另一种解决方案是您可以创建项目的 jar 文件,然后可以通过运行命令“java -jar <name_of_jar>.jar”来运行 jar 文件

    您可以通过以下方式创建一个 jar 文件File -> Project Structure -> Artifacts -> + -> JAR -> From modules with dependencies -> 选择主类 -> OK -> Build -> Build Artifacts然后就可以在project/out/artifacts/<name_of_jar>.jar中找到jar

【讨论】:

    【解决方案2】:

    你能发布项目结构和异常信息吗?

    【讨论】:

    • 我已经用目录结构和错误更新了我的问题
    • 这应该是评论,而不是答案。
    猜你喜欢
    • 1970-01-01
    • 2011-09-07
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2019-12-25
    • 2017-10-03
    • 1970-01-01
    相关资源
    最近更新 更多