Java 如何像在 Eclipse 中一样在 IntelliJ 中创建可运行的 JAR
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41746177/
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
How do I create a runnable JAR in IntelliJ as I would in Eclipse
提问by Nanor
My process for creating a runnable JAR from a project with many libraries with Eclipse has been.
我使用 Eclipse 从具有许多库的项目创建可运行 JAR 的过程是。
Export > Runnable JAR > Select launch configuration > Package required libraries into generated JAR > Finish
导出 > 可运行 JAR > 选择启动配置 > 将所需的库打包到生成的 JAR 中 > 完成
This creates a single JAR in my export destination which I FTP to my server and run fine.
这会在我的导出目标中创建一个 JAR,我将其 FTP 到我的服务器并运行良好。
I recently switched to IntelliJ for various reasons and it has been an improvement in all cases except for building my JARs which forces me to go back to eclipse temporarily.
我最近出于各种原因切换到 IntelliJ,除了构建我的 JAR 迫使我暂时回到 eclipse 之外,它在所有情况下都得到了改进。
With IntelliJ I:
使用 IntelliJ I:
Open Project Structure > Artifacts and create a new JAR from modules with dependencies. I then have my output layout with the name of the JAR, a META-INF inside it and all my libaries with the format Extracted <lib.jar/>
(sorry I can't upload screenshots on this VPN.)
打开 Project Structure > Artifacts 并从具有依赖项的模块创建一个新的 JAR。然后我的输出布局带有 JAR 的名称,里面有一个 META-INF 以及我所有的库的格式Extracted <lib.jar/>
(对不起,我不能在这个 VPN 上上传屏幕截图。)
I build my artifact (which is about 15MB bigger) and FTP it to my server, try to run it and I get the error:
我构建了我的工件(大约 15MB)并将其通过 FTP 传输到我的服务器,尝试运行它,但出现错误:
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
How do I mimic how I do it in Eclipse?
我如何模仿我在 Eclipse 中的做法?
EDIT
编辑
The two JARs that are created differ considerably:
创建的两个 JAR 有很大不同:
The valid jar that Eclipse creates looks like this:
采纳答案by Kirill
You're facing two issues one seems major and one is minor:
您面临两个问题,一个似乎是主要的,一个是次要的:
- Major: signature exception
- Minor: size of the jar is 15 MB greater than the jar produced by eclipse.
- 主要:签名异常
- 次要:jar 的大小比 eclipse 生成的 jar 大 15 MB。
The solution of both of the issues lies in the way you are building the artifact. First of all, remove all extracted *.jar
then add all the jar's from the available elements pan as shown in the figure.
这两个问题的解决方案在于您构建工件的方式。首先,删除 allextracted *.jar
然后从可用元素 pan 中添加所有 jar,如图所示。
It is obvious that adding packaged(compressed) jars only, will decrease the size. But it also solves the signing issue. For more explanation, please have a look at this article. I'll only quote one line.
很明显,仅添加打包(压缩)的 jar 会减小大小。但它也解决了签名问题。有关更多解释,请查看这篇文章。我只会引用一行。
It's probably best to keep the official jar as is and just add it as a dependency in the manifest file......
最好保持官方 jar 原样,并将其添加为清单文件中的依赖项......
It seems that some of the dependencies are signed and repackaging messes up the META-INF of your project.
似乎某些依赖项已签名并且重新打包会弄乱您项目的 META-INF。
回答by Ray Tayek
you could try using a gradle script. eclipse will make you a gradle project. you can use that build file with any ide. it has a built in jar task.
您可以尝试使用 gradle 脚本。eclipse 会让你成为一个 gradle 项目。您可以将该构建文件与任何 ide 一起使用。它有一个内置的 jar 任务。
回答by Kirill
You can these steps:
您可以执行以下步骤:
1) File -> Project Structure -> Project Settings -> Artifacts -> Jar -> From modules with dependencies
1) File -> Project Structure -> Project Settings -> Artifacts -> Jar -> From modules with dependencies
2) Check the Include in project build checkbox.
2) 选中包含在项目构建中复选框。
3) Right mouse click on module -> build module 'ModuleName'
3) 鼠标右键单击模块 -> 构建模块 'ModuleName'
回答by another
If you are using Maven you need to put
META-INF
directory under/main/resources
folder instead ofmain/java
.
如果您使用 Maven,则需要将
META-INF
目录放在/main/resources
文件夹下而不是main/java
.
Reference: Intellij - Nikolay Chashnikov
参考:Intellij - Nikolay Chashnikov
Then you can execute the runnable jar normally with java -jar filename.jar
然后你可以正常执行runnable jar java -jar filename.jar