java - 创建 jar 文件时出现问题

标签 java jar

我通常使用 Eclipse 来完成所有 Java 编程,当我使用它来测试我的程序时,它效果很好。到目前为止没有任何问题。当我尝试创建可执行 jar 文件时,我的问题出现了。无论我使用 Eclipse 来制作它还是通过命令来完成它,我总是会遇到相同的错误“无法找到主类:Main”我已经遵循了有关它的书面指南,甚至是一些 YouTube 视频一个直观的例子。为什么找不到主类?

最佳答案

尝试将 list 文件名更改为“MANIFEST.MF”

根据online specs for manifest files ,它需要特定位置的特定文件名。 jar 工具处理该位置,但如果您给它“manifest.txt”,它可能会保留该名称,然后 jar 加载机制会跳过该名称。

     The META-INF directory  

        The following files/directories in the META-INF directory are recognized and 
        interpreted by the Java 2 Platform to configure applications, extensions, 
        class loaders and services: 

     MANIFEST.MF
     The manifest file that is used to define extension and package related data.

尝试letting Eclipse generate the JAR file 。 Main 类应该位于与包结构匹配的子目录中。你的Main类有包吗?如果没有,请添加

封装测试;

到 Main.java 的顶部,然后确保 Main.class 文件在 jar 内创建为/test/main.class 。 MANIFEST.MF 可以有 reference to your Main.class对于 application's entry point如下:

Main-Class: The value of this attribute defines the relative path of the main application class which the launcher will load at startup time. The value must not have the .class extension appended to the class name.

[编辑]
请发布以下内容:
1)directory structure of the generated JAR (使用jar tf foo.jar)
2) list 内容来自/META-INF/MANIFEST.MF
3)具有静态main()方法的类的名称和包

关于java - 创建 jar 文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9946704/

相关文章:

java - Tomcat : PreResources tag not loading jars

java - 使用 Java 从可运行的 jar 中更改 Windows 7 桌面背景

java - contentView 不改变动态编程的内容。今天早上工作,我什至没有碰过它

java - Room 无法检索以下列

java - 如何将jar文件和dat文件转换为exe文件

java - 使用 Maven 创建一个包含已编译类和 javadoc 的 jar

java - PhoneGap错误: jar: invalid option -- 'e'

java - 是否有可能(并且明智地)在 JavaRDD 中执行其他 "spark-submit"?

java - 解释来自jboss实例的GC日志

java - 获取图像的 JPEG 渲染的内存表示