Java IntelliJ IDEA 中的错误 Manifest.mf 创建了 .jar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20952713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wrong Manifest.mf in IntelliJ IDEA created .jar
提问by grudolf
I'm trying to package a project using OptaPlanner 6.0.1 libraries into a .jar through IntelliJ IDEA's jar artifact but instead of my manifest.mf containing the standard
我正在尝试通过 IntelliJ IDEA 的 jar 工件将使用 OptaPlanner 6.0.1 库的项目打包到 .jar 中,而不是包含标准的 manifest.mf
Manifest-Version: 1.0
Main-Class: a.b.c.app
the jar uses the one supplied in ecj-3.7.2.jar, one of OptaPlanner's supporting libraries:
该 jar 使用 ecj-3.7.2.jar 中提供的那个,这是 OptaPlanner 的支持库之一:
Manifest-Version: 1.0
Build-Jdk: 1.6.0_26
Built-By: ibrandt
Created-By: Apache Maven
Archiver-Version: Plexus Archiver
Because of this, a "no main manifest attribute, in appname.jar"
error happens when trying to run the app. If I manually replace the manifest in .jar file with mine everything works correctly. Is there anything I can do to fix this?
因此,"no main manifest attribute, in appname.jar"
尝试运行应用程序时会发生错误。如果我用我的手动替换 .jar 文件中的清单,一切正常。我能做些什么来解决这个问题吗?
I keep the libraries in a separate /lib directory and they were added to the root of jar artifact as Extracted Directory, IntelliJ IDEA is v13.0.1.
我将库保存在单独的 /lib 目录中,并将它们作为提取目录添加到 jar 工件的根目录中,IntelliJ IDEA 是 v13.0.1。
采纳答案by jamhan
I had the same problem.
我有同样的问题。
Make sure your MANIFEST.MF is in:
确保您的 MANIFEST.MF 位于:
src/main/resources/META_INF/
NOT
不是
src/main/java/META_INF/
回答by Javaru
To fix:
修理:
- File > Project Structure
- Under Project Settingson the left, select "Artifacts"
- Find the JAR definition In the middle pane and select it
- In the left pane of the "Output Layout" tab find the jar file in the list and select it
- At the bottom, click the "Use Existing Manifest" button and select the manifest file that is in your project source.
- Click OK and run the build
- 文件 > 项目结构
- 在左侧的项目设置下,选择“工件”
- 在中间窗格中找到 JAR 定义并选择它
- 在“输出布局”选项卡的左窗格中,找到列表中的 jar 文件并选择它
- 在底部,单击“使用现有清单”按钮并选择项目源中的清单文件。
- 单击确定并运行构建
回答by Geoffrey De Smet
There are several ways to generate executable jars. Using IntelliJ's GUI feature is one good way. Another way is to use Maven(or similarly in gradle, buildr, etc) which is build-server friendly:
有几种方法可以生成可执行的 jar。使用 IntelliJ 的 GUI 功能是一种好方法。另一种方法是使用对构建服务器友好的Maven(或类似的在 gradle、buildr 等中):
It's more or less copy pasteable from the optaplanner examples maven build:
它或多或少可以从 optaplanner 示例 maven build 中复制粘贴:
- The end-user jar (optaplanner-examples-*.jar) must include the classpath of its dependencies in its manifest.
- The sh and bat scriptmust then run that jar with accordingly.
- 最终用户 jar (optaplanner-examples-*.jar) 必须在其 manifest 中包含其依赖项的类路径。
- sh 和 bat 脚本必须相应地运行该 jar。
回答by marcelocesar
I had a similar problem.
我有一个类似的问题。
The problem was in file pom.xml.
问题出在文件 pom.xml 中。
<archive>
<manifestEntries>
<Dependencies>one.jar,
two.rar,
other.jar
</Dependencies>
</manifestEntries>
</archive>
I do not know for what reason this code works in eclipse, but not in IntelliJ
我不知道为什么这段代码在 Eclipse 中有效,但在 IntelliJ 中无效
This it correct.
这是正确的。
<archive>
<manifestEntries>
<Dependencies>one.jar, two.rar, other.jar</Dependencies>
</manifestEntries>
</archive>
Manifest.mf worked!!!
Manifest.mf 有效!!!
I hope this helps.
我希望这有帮助。
回答by Autumn
As noted in @grudolf's comment in one of the other answers, one way to do this (and the only one that worked for me in an imported Gradle project) is to create an empty jar as follows:
正如@grudolf 在其他答案之一中的评论中指出的那样,一种方法(也是唯一一种在导入的 Gradle 项目中对我有用的方法)是创建一个空罐子,如下所示:
- Project Structure -> Artifacts -> + Jar -> Empty
- Centre pane now has Create Manifest and Use Existing Manifest buttons. Use one of these.
- I had difficulty if I extracted dependent libraries with their own manifests into the output root, they seemed to intermittently overwrite the new manually created manifest. Messing around with order of operations seemed to make it work.
- 项目结构 -> Artifacts -> + Jar -> Empty
- 中心窗格现在具有创建清单和使用现有清单按钮。使用其中之一。
- 如果我将带有自己的清单的依赖库提取到输出根目录中,我会遇到困难,它们似乎会间歇性地覆盖新的手动创建的清单。乱搞操作顺序似乎使它起作用。
UPDATE:
更新:
This is definitely a bug in Idea. This linked answerworks reliably when there are extracted directories. In essence, you find your .idea/JARNAME.xml, add add the following element to the very topof the <root>
element for your jar. Any extracted elements above your new file-copy that contain a manifest will clobber your new manifest.
这绝对是Idea中的一个错误。 当存在提取目录时,此链接答案可靠地工作。在本质上,你会发现你的.idea / JARNAME.xml,添加下面的元素添加到最顶端的的<root>
为你的jar元素。包含清单的新文件副本上方的任何提取元素都将破坏您的新清单。
<element id="directory" name="/META-INF">
<element id="file-copy" path="$PROJECT_DIR$/modulename/src/META-INF/MANIFEST.MF" />
</element>
回答by usbo
If you want to specify Main Class, you have to add this plugin to pom.xml:
如果要指定 Main Class,则必须在 pom.xml 中添加此插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<mainClass>Form</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
回答by Mohsen Abasi
To have no problem like Manifest, you should have a directory named "META-INF" in "src" directory. So, create it and put a file named "MANIFEST.MF" in it with the following contents:
为了没有像 Manifest 这样的问题,你应该在“src”目录中有一个名为“META-INF”的目录。因此,创建它并在其中放置一个名为“MANIFEST.MF”的文件,其中包含以下内容:
Manifest-Version: 1.0
Main-Class: <packageName>.Main
Not to forgot to replace the package's name containing Main class above!
不要忘记替换上面包含 Main 类的包名!