Java 使用 Intellij 2016 创建 Jar - 没有主要清单属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37776505/
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
Creating Jar with Intellij 2016 - No main manifest attribute
提问by mallaudin
I am getting no main manifest attribute
while running the jar create by Intellij. I extracted the jar and observed that there was anothermanifest file, not the one I had specified while creating artifact.
我no main manifest attribute
在运行 Intellij 创建的 jar时遇到了问题。我提取了 jar 并观察到还有另一个清单文件,而不是我在创建工件时指定的文件。
When I open manifest in IDE, it displays everything right but after creating jar I get a whole new manifest file.
当我在 IDE 中打开清单时,它会正确显示所有内容,但在创建 jar 后,我得到了一个全新的清单文件。
Manifest-Version: 1.0
Main-Class: YoutubeList
I tried every solution from other answers and still not getting it right. Why creating a simple jar is hell of a task in Intellij, it was supposed to help developers!
我尝试了其他答案中的所有解决方案,但仍然没有做对。为什么在 Intellij 中创建一个简单的 jar 是一项艰巨的任务,它应该帮助开发人员!
Edited
已编辑
And sometimes it does not include .class files in Jar which results in could not found or load class
有时它不会在 Jar 中包含 .class 文件,这会导致 could not found or load class
采纳答案by Zsolt Tolvaly
I was stucked with the same problem with maven build. When you are creating the artifact from project structure settings (ctrl+alt+shift+S), you have to change manifest directory:
我在 maven 构建中遇到了同样的问题。当您从项目结构设置(ctrl+alt+shift+S)创建工件时,您必须更改清单目录:
<project folder>\src\main\java
change java to resources
将 java 更改为资源
<project folder>\src\main\resources
I have also used the option extract to the target JAR, and it's working well.
我还对目标 JAR 使用了选项提取,它运行良好。
EDIT
编辑
You can find a detailed step-by-step, an other solutions here: https://stackoverflow.com/a/45303637/2640826
您可以在此处找到详细的分步说明和其他解决方案:https: //stackoverflow.com/a/45303637/2640826
回答by Per Huss
Under File
/ Project structure
/ Artifacts
you can specify how your jar is to be built. You can chose either to use an existing file as your manifest, or create a new one, in which case you specify main class and class path...
在File
/ Project structure
/Artifacts
你可以指定你的罐子是如何建造。您可以选择使用现有文件作为清单,也可以创建一个新文件,在这种情况下,您可以指定主类和类路径...
回答by Maksim Ryabovol
I spent a few days to resolve it. My solution: I loaded a project that present in this answer. Then I compared and corrected settings of the loaded project and my own project. I compared/corrected:
我花了几天时间来解决它。我的解决方案:我加载了一个出现在这个答案中的项目。然后我比较并更正了加载的项目和我自己的项目的设置。我比较/更正:
- Run/Debug Configurations
- MANIFEST.MF
- in Progect Structure settings: Project, Modules (mark what is sources, resources and etc), Artifacts.
- 运行/调试配置
- 清单文件
- 在项目结构设置中:项目、模块(标记什么是来源、资源等)、工件。
In the end, I placed META-INF in resources directory.
最后,我将 META-INF 放在资源目录中。
Maybe i did excess actions, but it worked for me :)
也许我做了多余的动作,但它对我有用:)
P.S. also need to choose "Inherit project compile output path" in Progect Structure settings -> Modules -> Path
PS还需要在Progect Structure settings -> Modules -> Path中选择“Inherit project compile output path”
回答by J.E.Tkaczyk
I found a different solution to the ones posted. In the IntelliJ Project Structure Dialog used to build the contents of the .jar file, I had to manually add the META-INF folder containing the Manifest file. Here is how it looks at the end in the project structure dialog box. If you don't see the folder in the list of jar contents, then the manifest file is not part of the jar file.
我找到了与发布的解决方案不同的解决方案。在用于构建 .jar 文件内容的 IntelliJ 项目结构对话框中,我必须手动添加包含清单文件的 META-INF 文件夹。下面是它在项目结构对话框中最后的样子。如果在 jar 内容列表中没有看到该文件夹,则清单文件不是 jar 文件的一部分。
The way I include the folder manually is to click the plus sign, select the Directory Content option and then navigate to the META-INF folder.
我手动包含文件夹的方法是单击加号,选择目录内容选项,然后导航到 META-INF 文件夹。
回答by Jool
If using Maven, Ensure your pom.xml has the main class referenced and fully qualified, similar to:
如果使用 Maven,请确保您的 pom.xml 具有引用和完全限定的主类,类似于:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<mainClass>org.mypkg.MyMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
(... of course the version number of the plugin may be different).
(...当然插件的版本号可能不同)。
The main class being not fully qualified, is what leads people to suggest moving the manifest to another location (in order to satisfy the reference locally).
主类不完全合格,导致人们建议将清单移动到另一个位置(以满足本地引用)。
回答by mallaudin
Actually I solved it by adding the following lines in build.gradle
实际上我通过添加以下几行来解决它 build.gradle
jar {
manifest {
attributes 'Main-Class': 'class name'
}
from {
configurations.compile.collect {
it.isDirectory()? it: zipTree(it)
}
}
}
回答by WesternGun
Putting the META-INF folder in */resources
can do, but is not the way: Intellij just puts all under main/resources
or test/resources
in the root level of the generated jar, that's why it works; but in a Java project, we usually put them under project root, which is the same level as src
. Putting them under "resources" is breaking the convention, besides, they are not resource files.
将 META-INF 文件夹放入*/resources
可以做到,但不是这样:Intellij 只是将所有内容放在生成的 jar 的根级别之下main/resources
或test/resources
中,这就是它起作用的原因;但是在Java项目中,我们通常将它们放在项目根目录下,与src
. 将它们放在“资源”下是违反约定的,此外,它们不是资源文件。
Make sure you:
确保你:
- use the existent
MANIFEST.MF
file at project root; - the main class is correct
- ticked the
Include in Project build
under "Project structure" -> "Artifacts" panel - have
META-INF
folder listed in the files to include in the jar, apart from "project compiled output", in theOutput Layout
tab - the generated file type is JAR, at right top corner
- 使用
MANIFEST.MF
项目根目录下的现有文件; - 主类是正确的
- 勾选
Include in Project build
下“项目结构”->“工件”面板 - 已
META-INF
在文件夹列在罐子里包括,除了“项目编译的输出”,在Output Layout
标签 - 生成的文件类型是JAR,在右上角
And then, save the settings, build again, and enter "Build" menu to "Build Artifacts..", and "build" the JAR. Check the generated jar again, run with java -jar
.
然后,保存设置,再次构建,进入“构建”菜单到“构建工件..”,并“构建”JAR。再次检查生成的jar,运行java -jar
。
回答by JoschJava
In my case I had some dependencies added in my jar, which itself generated a Manifest. I didn't get it to work straight out of Intellij. However, what you can do is open the jar file with a zip program and add the Main class yourself and then copy it back into the jar:
就我而言,我在 jar 中添加了一些依赖项,它本身生成了一个 Manifest。我没有让它直接从 Intellij 中工作。但是,您可以做的是使用 zip 程序打开 jar 文件并自己添加 Main 类,然后将其复制回 jar: