简体   繁体   English

创建 JAR 文件后,命令行返回“错误:无法找到或加载主 class 示例程序”?

[英]After creating JAR file, command line returns 'Error: Could not find or load main class ExampleProgram`?

JAR file will not run, instead returns JAR 文件不会运行,而是返回

Error: Could not find or load main class ExampleProgram
Caused by: java.lang.NoClassDefFoundError: ExampleProject/ExampleProgram (wrong name: ExampleProgram)

I've tried deleting the class file, then re-creating the JAR file.我试过删除 class 文件,然后重新创建 JAR 文件。 I've tried deleting and re-creating the program from scratch.我尝试从头开始删除并重新创建程序。 I've tried creating the JAR file without a manifest.我尝试创建没有清单的 JAR 文件。

package ExampleProject;

public class ExampleProgram {

    public static void main(String[] args) {

        System.out.println("Hello, World!");

    }

}

This is the exact process in how I tried to create the JAR file on the command-line.这是我尝试在命令行上创建 JAR 文件的确切过程。

Josephs-MBP:program jepappano4$ ls
ExampleProgram.class    ExampleProgram.java
Josephs-MBP:program jepappano4$ java ExampleProgram
Error: Could not find or load main class ExampleProgram
Caused by: java.lang.NoClassDefFoundError: ExampleProject/ExampleProgram (wrong name: ExampleProgram)
Josephs-MBP:program jepappano4$ ls
ExampleProgram.class    ExampleProgram.java
Josephs-MBP:program jepappano4$ ls
ExampleProgram.class    ExampleProgram.java manifest.mf
Josephs-MBP:program jepappano4$ jar -cvfm myprogram.jar manifest.mf *.class
added manifest
adding: ExampleProgram.class(in = 450) (out= 301)(deflated 33%)
Josephs-MBP:program jepappano4$ java -jar myprogram.jar
Error: Could not find or load main class ExampleProgram
Caused by: java.lang.NoClassDefFoundError: ExampleProject/ExampleProgram (wrong name: ExampleProgram)

I am following a tutorial on creating JAR files and I expected this process to work, but it doesn't.我正在关注有关创建 JAR 文件的教程,并且我希望此过程能够正常工作,但事实并非如此。 What am I doing incorrect here?我在这里做错了什么?

Not sure if this is important or not, however, I am using Java 11.不确定这是否重要,但是,我使用的是 Java 11。

The 'wrong name' error is the key error here. “错误名称”错误是这里的关键错误。 This is what it means:这就是它的意思:

In java, the full name of a class is its package, followed by a dot, followed by the classname.在java中,一个class的全称是它的package,后面是一个点,后面是类名。 So, your full name for this class is ExampleProject.ExampleProgram .所以,这个 class 你的全名是ExampleProject.ExampleProgram

To start them, you must put this full name in yor manifest: It needs to have an entry that looks like:要启动它们,您必须将此全名放入您的清单中:它需要有一个如下所示的条目:

Main-Class: ExampleProject.ExampleProgram

In addition, folder structure must match the dots.此外,文件夹结构必须与点匹配。 So, the jar file (which is really just a zip file) must contain inside it at the root level a directory named ExampleProject and this dir needs a file named ExampleProgram.class .因此,jar 文件(实际上只是一个 zip 文件)必须在根级别包含一个名为ExampleProject的目录,并且此目录需要一个名为ExampleProgram.class的文件。

You've gotten one or both of those wrong, causing this error.你已经得到了其中一个或两个错误,导致了这个错误。 You can check the structure of your jar file as follows:您可以检查 jar 文件的结构,如下所示:

jar tvf myjar.jar

you should see a listing for:你应该看到一个列表:

ExampleProject/ExampleProgram.class

in that precise location.在那个精确的位置。

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

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