简体   繁体   English

尝试运行 .jar 文件时出现“ClassNotFoundException”

[英]"ClassNotFoundException" while trying to run .jar file

I have a .jar that I built following the Oracle docs, using jar cfm hangman.jar Manifest.txt src/classes/app/Main.class .我有一个 .jar 是我按照 Oracle 文档构建的,使用jar cfm hangman.jar Manifest.txt src/classes/app/Main.class The manifest.txt file contains Main-Class as classes.app.Main , telling where my Main class is. manifest.txt 文件包含 Main-Class 作为classes.app.Main ,告诉我的 Main 类在哪里。 When executed, ClassNotFoundException is thrown, saying it couldn't find classes.app.Main .执行时,抛出 ClassNotFoundException ,表示找不到classes.app.Main I need help trying to understand what's wrong here.我需要帮助试图了解这里出了什么问题。 Is it the main class or maybe a missing classpath?它是主类还是缺少类路径?

Here's the project tree:这是项目树:

.
├── hangman.jar
├── Manifest.txt
├── README.md
└── src
    ├── app
    │   ├── Main.java
    │   ├── Player.java
    │   ├── Players.java
    │   ├── Play.java
    │   ├── Themes.java
    │   ├── Word.java
    │   └── Words.java
    └── classes
        └── app
            ├── Main.class
            ├── Play.class
            ├── Player.class
            ├── Players.class
            ├── Themes.class
            ├── Word.class
            └── Words.class


You don't show the code, but it is extremely likely that the package for your class is just app not classes.app , and classes is only a directory name to contain the class files, not actually part of the package hierarchy.您没有显示代码,但很可能您的类的包只是app而不是classes.app ,并且classes只是包含类文件的目录名称,实际上并不是包层次结构的一部分。 The name of a class file entry in a jar, OR the name of a class file relative to a classpath directory, must be exactly a directory path equal to the package hierarchy (if any) plus the class name and the suffix .class , with nothing added or removed. jar 中的类文件条目的名称,或相对于类路径目录的类文件的名称,必须正好是等于包层次结构(如果有)加上类名和后缀.class的目录路径,其中没有添加或删除任何内容。 This means your jar should be created by going to the classes directory and then adding the file(s) relative to that directory :这意味着您的 jar 应该通过转到classes目录然后添加相对于该目录的文件来创建:

 jar cfm hangman.jar Manifest.txt -C classes app/Main.class 

and the Main-class entry in the manifest should be app.Main .清单中的Main-class条目应该是app.Main If you only need main-class in the manifest and nothing else (except version, IIRC), you can have jar create it for you:如果您只需要清单中的主类而不需要其他任何东西(除了版本,IIRC),您可以让jar为您创建它:

 jar cfe hangman.jar app.Main -C classes app/Main.class

Also I note that there are other classes in your source tree.我还注意到您的源代码树中还有其他类。 If these classes are called or referenced from the Main class, directly or indirectly (ie nested), they must also be in the jar.如果从 Main 类直接或间接(即嵌套)调用或引用这些类,则它们也必须在 jar 中。 You probably want to use app/* instead, although it is possible you want or even need to be more selective.您可能希望改用app/* ,尽管您可能希望甚至需要更具选择性。

Meta: I thought this was covered in the standard tutorial, but although most of the pieces are there they aren't really pulled together anyplace I could find and refer to. Meta:我认为标准教程中已经涵盖了这一点,但是尽管大部分内容都在那里,但它们并没有在我能找到和参考的任何地方真正整合在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM